5

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"
   },
 }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Mr Jones
  • 181
  • 1
  • 3
  • 12
  • 1
    You can use the Firebase console to send notification to all the devices. – Anandroid Jul 18 '16 at 08:57
  • You can send device to device notification, see http://stackoverflow.com/questions/37435750/how-to-send-device-to-device-messages-using-firebase-cloud-messaging/41913555#41913555 – eurosecom Jan 28 '17 at 19:42

3 Answers3

7

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.

emportella
  • 323
  • 4
  • 9
3

You cannot send to "all devices".

Instead you can target a specific device, a group of devices or a topic when using the Firebase Cloud Messaging API. Alternatively you can send to a user segment, a specific device, or a topic when using the Firebase Notifications console.

With Firebase Notifications you can get closest, since there is a default user segment that includes all devices that have a specific app installed. So if you have an Android and an iOS app, that would be two notifications. But there's no programmatic way to send to such an audience (yet).

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    You can use the registration_ids field to send to 1000 tokens at a time, so you can send as many request as is necessary to send to all your users in batches of 1000. – Arthur Thompson Jul 18 '16 at 19:04
  • Correct @ArthurThompson, but that means you must have a list of the tokens. There is no master audience for "all devices" (as far as I know). – Frank van Puffelen Jul 18 '16 at 20:08
  • correct, there is no "all devices" target in the REST API. – Arthur Thompson Jul 18 '16 at 20:46
  • How do you send a notification to, say, all Android users?.In the Firebase console you can select the android app users as target for the notification, but I cannot do the same through the API. Simply setting `"to":"com.myapp"` doesn't do the job. – Gonzalo Jul 25 '16 at 19:18
  • There is currently no API for Firebase Notifications, but we're considering adding one based on requests like yours. For the moment your best options to send to all users are to either subscribe all devices to a topic or to send all tokens to a servers and then use those to send to all users. – Frank van Puffelen Jul 25 '16 at 23:30
  • @frank-van-puffelen, Any news on that? We can only select topics with the API right now. – Dominique Alexandre Aug 24 '16 at 19:41
0

You can use the Firebase console to send notification to all the devices.

Anandroid
  • 403
  • 4
  • 14