Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 608
Ajnas Askar Avatar asked Nov 10 '18 04:11

Ajnas Askar


2 Answers

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

like image 176
Aravindh Kumar Avatar answered Oct 05 '22 01:10

Aravindh Kumar


I could do it using the following code:

await FirebaseMessaging.instance.subscribeToTopic('TopicToListen');
like image 34
Alexandre Alves Avatar answered Oct 05 '22 01:10

Alexandre Alves