the1.7gpaprogrammer
the1.7gpaprogrammer

Reputation: 115

Sending Notifications in to multiple users FCM

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.

Upvotes: 8

Views: 12352

Answers (2)

dnit13
dnit13

Reputation: 2496

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.

Upvotes: 9

hteshkumar
hteshkumar

Reputation: 31

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

Upvotes: 1

Related Questions