I'm a new one in a flutter and in my app, I need to implement FCM with global or with a topic subscription. I successfully implemented the FCM with device token but need to send a notification to all device. how can we fix this?
Asked
Active
Viewed 2.4k times
3 Answers
43
You can use subscribeToTopic to send a notification to all devices on login success or somewhere where you want to subscribe. sample code:
FirebaseMessaging firebaseMessaging = new FirebaseMessaging();
void fcmSubscribe() {
firebaseMessaging.subscribeToTopic('TopicToListen');
}
void fcmUnSubscribe() {
firebaseMessaging.unsubscribeFromTopic('TopicToListen');
}
Test the topic subscription by using firebase console to send the notification to a topic that the device is listening by choosing the topic in target

Zvi Karp
- 3,621
- 3
- 25
- 40

Aravindh Kumar
- 1,213
- 11
- 22
-
1While calling subscribeToTopic got java.lang.NoSuchMethodError: FirebaseMessaging.subscribeToTopic(Unknown Source). – Mashhood Qadeer Feb 08 '19 at 19:37
-
is there any way to do that on flutter web – Abdulmalek Dery Jun 15 '21 at 14:39
-
@AbdulmalekDery I haven't tried out web part, I hope someone can can answer this. – Aravindh Kumar Jun 16 '21 at 08:05
-
Can you please update the answer to match the current version of flutter – Francisca Mkina Dec 01 '21 at 14:39
-
I had to change this to `FirebaseMessaging.instance.subscribeToTopic` to avoid the 'NoSuchMethod' error – Dennis de Best Feb 23 '22 at 10:48
10
I could do it using the following code:
await FirebaseMessaging.instance.subscribeToTopic('TopicToListen');

Alexandre Alves
- 196
- 4
- 5
2
For web version in flutter you need to call the api for topic
Uri.parse('https://iid.googleapis.com/iid/v1/'+_token+'/rel/topics/'+topic),
headers: <String, String>{
'Content-Type': 'application/json',
'Authorization':
'key=YOUR_FCM_KEY'
};
token is the firebase generated token

Stefano Sansone
- 2,377
- 7
- 20
- 39

Shatabhishek Baidya
- 21
- 3
-
can you provide some more information on where to put this and how to use it? thx – LeptonByte Mar 15 '22 at 17:57
-
This is api call which you can call single time when you app starts – Shatabhishek Baidya Nov 05 '22 at 19:21