Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 1000
the1.7gpaprogrammer Avatar asked Aug 06 '16 21:08

the1.7gpaprogrammer


2 Answers

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.

like image 150
dnit13 Avatar answered Oct 31 '22 04:10

dnit13


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

like image 44
hteshkumar Avatar answered Oct 31 '22 05:10

hteshkumar