1

I want to send a notification from Firebase to a specific Flutter mobile device.

The logic (in pseudocode) would look like this:

Firebase function onSomethingHappening (interestedCientId){
  writeToFirestore // this part is easy
  notifyClientDevice (interestedCientId)
}

I know Firebase has Messaging ability, but it seems to broadcast the message to all devices.

Is there a way through Messaging (or any other Firebase means) to send a message to a specific device?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
skiffrace
  • 21
  • 4

2 Answers2

2

to send push notification to a specific device your have to get the FCM token from devices ad save that Token somewhere in the server or in firebase realtime or firestore. then you can send notification based on the that token to that specific device.

Please read the following documentations:

https://firebase.flutter.dev/docs/messaging/notifications/

https://firebase.google.com/docs/cloud-messaging/flutter/client

Here is the simple code

FirebaseMessaging.instance.onTokenRefresh
    .listen((fcmToken) {
      // TODO: If necessary send token to application server.
      // Note: This callback is fired at each app startup and whenever a new token is generated.

    })
    .onError((err) {
      // Error getting token.
    });

with this token you can send notification form firebase console or the this API

https://fcm.googleapis.com/fcm/send

    {
        "to": "YOUR_FCM_TOEKEN",
        "notification": {
            "title": "HI DEVELOPER I AM TEWST",
            "body": "Hi DEVELOPER"
        }
    }

METHOD FOR THIS IS POST

Engr.Aftab Ufaq
  • 3,356
  • 3
  • 21
  • 47
  • Renaud & Engr - thank you for your reply. It worked like a charm on the first try! BTW - someone at stackoverflow closed my question as a duplicate. The question may have been a duplicate, but your answers are not - they are far better - more specific, and point to working code, unlike the answers to previous questions, which are just a bunch of json – skiffrace Jun 12 '23 at 02:38
  • upvote and accept my answer. thanks @skiffracec – Engr.Aftab Ufaq Jun 12 '23 at 11:03
  • I tried to accept both answers, but it appears that stack allows only one (why?). When I accepted yours, it unaccepted the other one. – skiffrace Jun 13 '23 at 02:27
1

Is there a way through Messaging (or any other Firebase means) to send a message to a specific device?

Yes, have a look at the "Send messages to specific devices" section in the Firebase Cloud Messaging documentation.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121