Search code examples
firebasefirebase-cloud-messagingpostman

Send notification for group of users in firebase by REST API


I'm trying to send notification of list of users, is this applicable in firebase REST API. what I tried and succeeded is send a notification for a specific user and broadcast notification but I didn't find a sample request for a group of users!

below is the request I used for Broadcast by POSTMAN :

URL :

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

Body:

{ "data": 
{
 "title": "Firebase notification",
 "detail": "I am firebase notification. you can customise me. enjoy"
 },
 "notification":{
      "title":"Hi from Postman ",
      "body":"great match!"
    },
 "to":"/topics/all"
// I don't think to could accept an array of the users device token 
 }

Solution

  • Firebase Cloud Message doesn't have the concept of a user, nor of a group of users. It instead has two ways to target messages:

    1. You can send messages to one or more FCM tokens, which is a token each installation of the app generates. By sending messages to tokens, the sender determines who they send the message to.
    2. You can send messages to topics, which app instances subscribe to. By sending messages to topics, the subscriber can determine which messages they receive.

    Neither of these is pertinently better than the other, as they both have their own use-cases. The choice is typically one of convenience versus control.

    • If you send the message to a topic, you have to assume that all your users can receive the message (as they are the ones subscribing to topics).
    • If you want full control over who receives the message, you should keep a list of FCM tokens on your server, and pass the tokens to deliver the message into the API call.