Firebase Cloud Messaging - Send Message to All Users

How do you send a Firebase Notification to all devices via CURL?

EDIT: It appears that this method is not supported anymore (thx to @FernandoZamperin). Please take a look at the other answers!

Instead of subscribing to a topic you could instead make use of the condition key and send messages to instances, that are not in a group. Your data might look something like this:

{
"data": {
"foo": "bar"
},
"condition": "!('anytopicyoudontwanttouse' in topics)"
}

See https://firebase.google.com/docs/cloud-messaging/send-message#send_messages_to_topics_2

Firebase Cloud Messaging - Send message to all users

You can make use of topics. Given that all of your users are subscribed to a specific one. Just like what I mentioned here (removed some parts, just check them out if you want):

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.

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

Firebase Cloud Messaging: how to send data message to all users?

Use topic messaging. You can define the name of a topic that all installations of your app will subscribe to, then send the message to that topic.

You can use the Firebase Admin SDK from your server to send that message. Or you can use the FCM HTTP API to send that message.

Firebase Messaging: Send a data message to all app users with a cloud function

I believe the only part you will need to change is the end. You won't need this part here admin.database().ref("/messages/" + context.params.meta).remove().

For the messaging, your code needs to look something like the below sample:

// Send a message to devices subscribed to the provided topic.
admin.messaging().send(payload)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
})
.catch((error) => {
console.log('Error sending message:', error);
});

You need to user a catch to manage the error - this way you will be able to visualize as well, what might be causing your issue. You can find more information in this documentation here: Send messages to topics.

Besides that, I found this nice repository - that you can access here - with some examples and more code samples, on how to use Cloud Functions with FCM.

Let me know if the information helped you!

Push notification to all users in firebase

If you absolutely can't use a topic, then you will need to collect all the device ID tokens from all your users' app installations, then send the message directly to all those tokens. But I suspect it will be easier to user a topic instead of writing all that code.

How to send FirebaseMessaging dynamically to all users when one action is done - Flutter?

You can do this using a cloud function. Either by calling the function from your app when the publication is created, or by having the cloud function listen for a new document. See this Medium post for some ideas: https://medium.com/@jerinamathews/send-firebase-cloud-messaging-fcm-to-a-topic-using-cloud-function-when-realtime-database-value-fa78fa758549

Update

Based on your update, I suggest you look at using a 'Topic' rather than a specific token (that applies to only one device). To use a Topic you need to ensure all users automatically subscribe to the chosen topic when they open the app (they can subscribe to the same topic each time, it has no negative impact). FCM maintains a list of subscribed device tokens against the topic.

I haven't used topic via an http post so I cannot confirm it is possible but I am assuming if you can send to a token you must be able to send to a topic.

Can I Send notifications to specific users in firebase cloud messaging console

You can send notifications from the console to specific users by following the steps below:

  1. Get the FCM registration tokens for the users' devices. You can check how to get the tokens for the devices depending on the platform here.
  2. Go to the project's Notification Composer.
  3. Enter the notification details i.e notification title(optional),
    notification text, notification image(optional), notification name(optional)
    .
  4. Click Send test message.
  5. Enter the FCM registration tokens.
  6. Click Test.

Pushing Notification to all users in Firebase

First of all you need to understand a topic does not need to create (it will be create automatically), you only need to define the topic name for example if you are creating app to receive push notification when the weather change, so the topic name could be "weather".

Now you need have 2 components: mobile & backend

1. Mobile: in your mobile app you only need integrate the Firebase SDK and subscribe to the topic "weather" how do you do that?

Firebase.messaging.subscribeToTopic("weather")

Don't forget checking documentation.

2. Backend: in your server you will need to implement the sender script based on FCM SDK.
If you are a beginner I'd recommend you use Postman to send push notifications and then integrate FCM in your backend app.

You can send this payload trough Postman (don't forget set your API KEY in headers)

https://fcm.googleapis.com/fcm/send

{
"to": "/topics/weather",
"notification": {
"title": "The weather changed",
"body": "27 °C"
}
}

If that works, you can add FCM SDK to your backend:

$ sudo pip install firebase-admin
default_app = firebase_admin.initialize_app()

Finally you can send notifications as documentation says:

from firebase_admin import messaging

topic = 'weather'

message = messaging.Message(
notification={
'title': 'The weather changed',
'body': '27 °C',
},
topic=topic,
)
response = messaging.send(message)

More details here: https://github.com/firebase/firebase-admin-python/blob/eefc31b67bc8ad50a734a7bb0a52f56716e0e4d7/snippets/messaging/cloud_messaging.py#L24-L40

You need to be patient with the documentation, I hope I've helped.

How to send FCM messages to a different user

Firebase Cloud Messaging has three ways to target messages:

  1. You send a message to a FCM token/device ID.

    An FCM token identifies a specific app on a specific device, so you're targeting a single app instance.

  2. You send a message to a topic.

    An app instance can subscribe to any topic, so you're targeting everyone who has subscribed to the topic here.

  3. You send a message to a device group.

    You can group FCM tokens into groups of up to 20, which you can then target with a single message.

Of these options, the first and third are by far the most common.

You'll note that there's no options to send a message to a user, as FCM doesn't have the concept of a user. A user can be signed in to the same app on multiple devices, in which case they'd have multiple FCM tokens. And (less common, but still common enough to think about) multiple users can sign into the same app on the same device.

You'll want to set up a database somewhere (sometimes referred to as a token registry) where you map the FCM tokens to how you want to send messages. So in your case, storing the tokens for each user makes sense, and is in fact quite idiomatic.

How to send firebase to multiple users with individual notification content

Each request to FCM to send a message, will send the exact same message to all recipients in that call. There is no personalization of the messages for individual recipients within a single call.

If you want to send personalized messages to recipients, you'll need to make a separate call for each message variant.


Update: if you use the newer versioned API to send a message to multiple devices, you can now specify a different message body for each user.



Related Topics



Leave a reply



Submit