Sending Notifications in to multiple users FCM

11,114

Solution 1

To send FCM to multiple device you use the key "registration_ids" instead of "to"

"registration_ids": ["fcm_token1", "fcm_token2"]

Have a look at this package and see how they implemented it.

Solution 2

Instead of "to":"device_id" you should use "to":"topic" ,

topic is use from group messaging in FCM or GCM

https://developers.google.com/cloud-messaging/topic-messaging

Share:
11,114
the1.7gpaprogrammer
Author by

the1.7gpaprogrammer

Updated on June 17, 2022

Comments

  • the1.7gpaprogrammer
    the1.7gpaprogrammer about 2 years

    I am configuring my mobile applications with firebase cloud messaging. I've finally figured out how to send these annoying to configure notifications. My python code looks like this

    url = 'https://fcm.googleapis.com/fcm/send'
    body = {  
    "data":{  
       "title":"mytitle",
       "body":"mybody",
      "url":"myurl"
    },
    "notification":{  
      "title":"My web app name",
      "body":"message",
      "content_available": "true"
    },
     "to":"device_id_here"
     }
    
    
    headers = {"Content-Type":"application/json",
            "Authorization": "key=api_key_here"}
    requests.post(url, data=json.dumps(body), headers=headers)
    

    I would think that putting this in a for loop and swapping device ids to send thousands of notifications would be an immense strain on the server and a bad programming practice. (Correct me if i'm wrong) now the documentation tells me to create "device groups" https://firebase.google.com/docs/cloud-messaging/notifications which store device_id's to send in bulk....this is annoying and inefficient. As my groups for my web application are constantly changing.

    Plain and Simple

    How do I send the notification above to an array of device id's that I specify in my python code so that i can make only 1 post to FCM instead of thousands.