How to send notifications to all devices using Firebase Cloud Messaging

10,885

Solution 1

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.

Solution 2

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).

Share:
10,885
Mr Jones
Author by

Mr Jones

Updated on June 14, 2022

Comments

  • Mr Jones
    Mr Jones about 2 years

    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"
       },
     }
    
    • Anandroid
      Anandroid almost 8 years
      You can use the Firebase console to send notification to all the devices.
    • eurosecom
      eurosecom over 7 years
      You can send device to device notification, see stackoverflow.com/questions/37435750/…
  • Arthur Thompson
    Arthur Thompson almost 8 years
    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.
  • Frank van Puffelen
    Frank van Puffelen almost 8 years
    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).
  • Arthur Thompson
    Arthur Thompson almost 8 years
    correct, there is no "all devices" target in the REST API.
  • Gonzalo
    Gonzalo almost 8 years
    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.
  • Frank van Puffelen
    Frank van Puffelen almost 8 years
    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.
  • Dominique Alexandre
    Dominique Alexandre almost 8 years
    @frank-van-puffelen, Any news on that? We can only select topics with the API right now.
  • Nitin Sidhu
    Nitin Sidhu about 4 years
    i couldn't find the option. where i can send. can please help .?