3

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?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Shravan Jain
  • 43
  • 1
  • 1
  • 7
  • Possible duplicate of [FCM (Firebase Cloud Messaging) how to send to all Phones?](http://stackoverflow.com/questions/37634563/fcm-firebase-cloud-messaging-how-to-send-to-all-phones) – AL. Sep 26 '16 at 01:54
  • @AL. I'm afraid it's not a duplicate bro. Device messaging is different from Topic based messaging, but yes, it's another workaround. Thanks for sharing. – looptheloop88 Sep 26 '16 at 07:52
  • @looptheloop88 It's not a total duplicate, however, the answer in that post pretty much answers this one. Although Device Group Messaging is a way to send to multiple devices, it's use-case is mostly different. It's not really a convenient way to use it to send it for all users, but it can be used for user segments. – AL. Sep 26 '16 at 07:55
  • I beg to disagree. Both are convenient ways to send messages and require some additional work to do. It's almost the same. If the app is already in the production and the developer doesn't want to add additional codes to subscribe in a topic, then he still needs to add the registration ids to a topic as described in this documentation https://developers.google.com/instance-id/reference/server#manage_relationship_maps_for_multiple_app_instances – looptheloop88 Sep 26 '16 at 08:29
  • 1
    @looptheloop88 He does. I'm not saying he can't use it. Just that the Device Group Messaging is not commonly used for that use-case, as you've mentioned in your answer -- *Typically, "group" refers a set of different devices that belong to a **single user***. Use-case wise, Topic Messaging is the way to go. – AL. Sep 26 '16 at 09:14
  • Hey Guys, thanks for helping me. i only want the JSON format by which i can send the request to multiple devices. Rest of the things, i have. – Shravan Jain Sep 26 '16 at 20:04
  • Like others have suggested, topic messaging seems to be the way for you to go, so the to value would be: "to": "/topics/" the rest will be the same – Arthur Thompson Sep 27 '16 at 21:22

3 Answers3

3

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.

Diogo Domanski
  • 483
  • 1
  • 4
  • 7
2

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.

Community
  • 1
  • 1
AL.
  • 36,815
  • 10
  • 142
  • 281
0

With Firebase Device Group Messaging, you can send a single message to multiple instances of an app running on devices belonging to a group. Typically, "group" refers a set of different devices that belong to a single user. All devices in a group share a common notification key, which is the token that FCM uses to fan out messages to all devices in the group.

The steps and guidelines altogether with the JSON payload are in the Firebase documentation. Please check that out.

On the other hand, you can send messages to devices registered in an app via Firebase Notification console. You'll just need to compose a new message and select the app in the Target section.

looptheloop88
  • 3,026
  • 17
  • 20
  • 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. – Shravan Jain Sep 26 '16 at 06:36
  • @ShravanJain - please check the link of device messaging. – looptheloop88 Sep 26 '16 at 07:36
  • Okay. I'll check that. – Shravan Jain Sep 26 '16 at 20:02