Ajnas Askar
Ajnas Askar

Reputation: 1093

How to subscribe to a topic in flutter FCM?

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?

Upvotes: 24

Views: 28142

Answers (3)

Shatabhishek Baidya
Shatabhishek Baidya

Reputation: 21

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

Upvotes: 2

Alexandre Alves
Alexandre Alves

Reputation: 306

I could do it using the following code:

await FirebaseMessaging.instance.subscribeToTopic('TopicToListen');

Upvotes: 11

Aravindh Kumar
Aravindh Kumar

Reputation: 1243

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

Upvotes: 44

Related Questions