3

I want a code to send notification from one device to multiple devices on a specific topic and I want to show that notification on devices who subscribe to that topic? I will use firestore to store data and store tokens and also use Firebase messaging to send notifications

Mr vd
  • 432
  • 4
  • 14

4 Answers4

4

This is my code to send notification on particular topic

I hope this helps the new developer.

import 'package:http/http.dart' as http;

Future<void> sendNotification(subject,title) async{

final postUrl = 'https://fcm.googleapis.com/fcm/send';

String toParams = "/topics/"+'yourTopicName';

final data = {
"notification": {"body":subject, "title":title},
"priority": "high",
"data": {
"click_action": "FLUTTER_NOTIFICATION_CLICK",
"id": "1",
"status": "done",
"sound": 'default',
"screen": "yourTopicName",
},
"to": "${toParams}"};

final headers = {
'content-type': 'application/json',
'Authorization': 'key=key'

};

final response = await http.post(postUrl,
body: json.encode(data),
encoding: Encoding.getByName('utf-8'),
headers: headers);

if (response.statusCode == 200) {
// on success do 
print("true");
} else {
// on failure do 
print("false");

}
}

Subscribe by using

FirebaseMessaging _firebaseMessaging =  FirebaseMessaging();
_firebaseMessaging.subscribeToTopic("yourTopicName");
Mr vd
  • 432
  • 4
  • 14
3

You can subscribe to a topic using firebase_messaging and the FirebaseMessaging.subscribeToTopic:

FirebaseMessaging().subcribeToTopic('topic_name');

You can send notifications to a topic using either the Firebase Console or some backend code, e.g. in Cloud Functions.

Learn more.

creativecreatorormaybenot
  • 62,078
  • 29
  • 174
  • 277
3

Sending a message to a device require that you call the Firebase Cloud Messaging API and specify the FCM Server Key. As its name implies, this key should only be used on a trusted environment, such as your development machine, a server that you control, or an environment like Cloud Functions. The reason this is required is that anyone who has you FCM server key can send messages to all users of your app.

The simplest way to get started is to simply run a curl command or something like that, calling the legacy FCM REST API. See an example of that here: How can I send a Firebase Cloud Messaging notification without use the Firebase Console? To send to a topic, make sure the to value is something like "/topics/your_topic".

For a more production level, you'll probably want to introduce a server, or use Cloud Functions. Sending a message then becomes a multi-step process like:

  1. The client that wants to send a message, writes that messages to a database, or calls an API.
  2. This write operation triggers your server, or Cloud Functions, which validates the request (determining that this user is authorized to send this message).
  3. The server-side code then calls the Firebase Admin API to send a message to a topic.

For one example of this, see this folder in the functions-samples repo.

Also see:

Frank van Puffelen
  • 418,229
  • 62
  • 649
  • 645
1

According to firebase_messaging readme page, in the last section, you can not send a cloud message using the flutter firebase_messaging library read the Sending Message.

To subscribe a user to a topic:

FirebaseMessaging _firebaseMessaging =  FirebaseMessaging();
_firebaseMessaging.subscribeToTopic("MyTopic");

This will subscribe that device to the topic MyTopic.

You can also unsubscribe by:

_firebaseMessaging.unsubscribeFromTopic("MyTopic");
Ahmed AL-Yousif
  • 360
  • 1
  • 9
  • I would like to know what happens if the user uninstall the app and reinstall it, he will lose all topics subscribed? Will have to subscribe all them again? thanks! – Jorge Vieira Apr 19 '20 at 13:00
  • check [this](https://firebase.google.com/docs/cloud-messaging/android/client#sample-register). when the user subscribe this means that his registration token that is unique to his instance of the app will be linked to the topic, and in the doc you will see that this registration token will change if the app is uninstalled and reinstalled. – Ahmed AL-Yousif Apr 20 '20 at 14:16
  • So i will have to register that people again if the token change, right? – Jorge Vieira Apr 21 '20 at 15:00
  • 1
    Yes, you do. It is like if they were signed in and deleted the app, the would have to sign in again when the re-download the app. – Ahmed AL-Yousif Apr 22 '20 at 11:47