JSON Data to send Push Notification to all of the registered devices using Firebase

12,902

Solution 1

If you are looking for a payload parameter to specify that you intend the message for all your users, unfortunately, it doesn't exist.

Commonly, when sending notifications to multiple users, you can make use of the registration_ids parameter instead of to. However, it only has a maximum of 1000 registration tokens allowed. If you intend to use this, you can make batch requests of 1000 registration tokens each, iterating over all the registration tokens you've stored in your app server.

As already mentioned by @looptheloop88, you can make use of the Firebase Console to send a message to all the users of a specific app, but if you're planning to send it via your own App Server, the most convenient way you can do is make use of is Topic Messaging. As per the answer in the possible duplicate post I commented by @DiegoGiorgini:

Sending a message to all the phones like what you do from the Firebase Web Console is only possible from the Web Console. If you need this feature from the API you can submit a feature request: https://firebase.google.com/support/contact/bugs-features/

Another possibility is to have all the client registering to a specific topic via FirebaseMessaging.getInstance().subscribeToTopic(topicName)

In this way you can send a message to the whole topic without collecting the registration-ids manually.

However, do keep in mind that Diagnostics for messages sent to Topics are not supported.

Solution 2

I had the same need and I solved it by setting to to /topics/all and restricted_package_name to my package's name:

{
    "to": "/topics/all",
    "restricted_package_name": "<PACKAGE_NAME>",
    "notification": {
        "title": "<TITLE>",
        "body": "<BODY>",
        "click_action": "FCM_PLUGIN_ACTIVITY"
    }
}

Every message I send is receive by mobile apps, without having to programmatically subscribe to a topic.

Share:
12,902
Shravan Jain
Author by

Shravan Jain

Updated on June 04, 2022

Comments

  • Shravan Jain
    Shravan Jain over 1 year

    This is the JSON structure when I need to send the notification to specific device using Firebase. So how should I modify it to send the same notification to all of the devices or couple of selected devices?

    { "notification": {
        "title": "Quiz App",
        "text": "Your Quiz has submitted Successfully."
      },
      "to" : "Unique Key"
    }
    

    What will be the JSON structure for sending the push notification to all of the devices using Firebase in android?

  • Shravan Jain
    Shravan Jain about 7 years
    Hi @looptheloop88 thanks for answering my question. Actually, i already did this thing from Firebase console but if i need to send it from my web server then i need to send the JSON from my server to Firebase server. Firebase team documented the format of JSON for sending the messaging to specific device but not for sending to all of the devices or more than one selected devices.
  • looptheloop88
    looptheloop88 about 7 years
    @ShravanJain - please check the link of device messaging.
  • Shravan Jain
    Shravan Jain about 7 years
    Okay. I'll check that.