Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send notifications to all devices using Firebase Cloud Messaging

On the Firebase doc, it always has a "to" field with a device/token id... but how can I get it to send the notification to all devices. What do I replace the to field with, or what value do I put in there. I'd removed it altogether but it comes back with an error asking for it. Any ideas?

{
   "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
   "data" : {
     "Nick" : "Mario",
     "body" : "great match!",
     "Room" : "PortugalVSDenmark"
   },
 }
like image 477
Mr Jones Avatar asked Jul 18 '16 01:07

Mr Jones


People also ask

Is firebase Cloud Messaging push notifications?

Firebase Cloud Messaging (FCM) provides a reliable and battery-efficient connection between your server and devices that allows you to deliver and receive messages and notifications on iOS, Android, and the web at no cost.


1 Answers

As Frank van Puffelen said there isn't a way to send to all devices. What you could do is to subscribe everyone to a single topic in the app side using:

FirebaseMessaging.getInstance().subscribeToTopic("TopicName");

So your message would look like this:

  {
   "to"   : "/topics/TopicName",
   "data" : {
     "Nick" : "Mario",
     "body" : "great match!",
     "Room" : "PortugalVSDenmark"
   },
 }

The topic is automatically created in the Firebase server side, but sometimes it takes a while to show up in the Firebase drop down list.

The number of users in a single topic are unlimited.

like image 190
emportella Avatar answered Sep 28 '22 16:09

emportella