Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Messaging notification for specific device in flutter

I am working on a flutter application whereby a lot of server requests were implemented on our custom server including login/signup. However, we decided to use Firebase Cloud Messaging for notifications. For example, included in the application is a chat feature.

During end to end messaging, as messages get posted to our server, users should receive notifications on their specific devices. I have already successfully implemented and configured firebase within the app and can send messages from the console but i need to make it work on specific devices.

// Replace with server token from firebase console settings.
final String serverToken = '<Server-Token>';
final FirebaseMessaging firebaseMessaging = FirebaseMessaging();

Future<Map<String, dynamic>> sendAndRetrieveMessage() async {
  await firebaseMessaging.requestNotificationPermissions(
    const IosNotificationSettings(sound: true, badge: true, alert: true, provisional: false),
  );

  await http.post(
    'https://fcm.googleapis.com/fcm/send',
     headers: <String, String>{
       'Content-Type': 'application/json',
       'Authorization': 'key=$serverToken',
     },
     body: jsonEncode(
     <String, dynamic>{
       'notification': <String, dynamic>{
         'body': 'this is a body',
         'title': 'this is a title'
       },
       'priority': 'high',
       'data': <String, dynamic>{
         'click_action': 'FLUTTER_NOTIFICATION_CLICK',
         'id': '1',
         'status': 'done'
       },
       'to': await firebaseMessaging.getToken(),
     },
    ),
  );

This is the documented way of sending messages within flutter. However, this will send notifications to all users not a single user. So, how do i target a specific user?

like image 209
custom apps Avatar asked Oct 21 '25 04:10

custom apps


2 Answers

I have read some implementations for what you are trying to achieve and I guess you should follow this steps:

  1. Install Firebase Cloud Messaging (FCM) for Flutter (I see you already done it)
  2. Get the device firebase messaging token by calling the "getToken" method provided by FCM
  3. Save this token when user logs in on a document of Firebase Cloud Firestore to associate this device token to the user (or create one array of tokens for the user so you can have multiple devices per user
  4. Monitor when messaging happens on your database. When it happens, gather the destination user, get the token(s) associated to this user and send one FCM to that specific device id(s). For this point, I suggest you use Firebase Cloud Functions despite all the duplication of data between servers that this may entail (ex: your server has the user name with some id and that same id "lives" in some noSQL document inside Firestore)

Hope it helps. I provide no code for any of this because I believe it is not what you are searching for.

like image 123
Ricardo Rodrigues Avatar answered Oct 24 '25 09:10

Ricardo Rodrigues


You need change

'to': await firebaseMessaging.getToken(),

to

'to': '${token of specific devices}', .

like image 43
redgreen Avatar answered Oct 24 '25 09:10

redgreen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!