I'm using Xamarin and Firebase Cloud Messaging to send push notifications to mobile devices.
I have notification settings where the user can allow and block specific notification types. So I made topics A, B, C for example. If the user allows notifications of type A, then the device will subscribe to topic A. I want to send a unique notification to that device if it is subscribed to notifications of type A. However, it seems that I cannot add two parameters to 'to: '
Currently I have:
{
"to": "{device_token}",
"data": {
"message": "hello",
},
"priority": "high"
}
I tried to add "condition": 'A' in topics but it doesn't allow me to have both 'to' and 'condition'.
You can't combine topics and tokens in the way you're trying. If you send a message to a topic, the message is delivered to every instance that is subscribed to that topic. If you send a message to a device token, it is only delivered to that device.
Some options I can think of:
Send a data message, and then detect in the client whether to show it or not.
Build your own message targeting system on top of device instance IDs.
Related
I am creating an app in which users can subscribe to multiple topics.
Whenever a news article is shared within the app, it is assigned multiple topics.
Users receive push notifications whenever a news article that is relevant to a topic they subscribed to is shared.
Say a user subscribed to topics A and B.
A news article that belongs to both of these topics (A & B) gets shared.
In this case, what happens in my code is,
for(const topic of topics) {
...
admin.messaging().send(message);
...
}
So the user will receive two push notifications, one for topic A and one for topic B.
Since I'm sending FCM push notifications not to specific devices but to topics, I cannot keep track of who received the push notifications, so I cannot prevent duplicate notifications manually.
Would there be a way for me to make sure the user does not receive the same fcm push notification twice (i.e. receive the push notification only for topicA and not for topic B)?
I am handling both the server-side (TypeScript) and client-side (Flutter), so a solution for either side will be greatly appreciated!
What you want to use is a single call to send to send to all topics using a condition.
Given your example of topic A and topic B, that'd be:
const condition = '\'stock-GOOG\' in topics || \'industry-tech\' in topics';
const message = ;
admin.messaging().send({
notification: {
title: 'Your notification title',
body: 'Your notification body'
},
condition: "'TopicA' in topics ||'TopicB' in topics"
})
In Urban Airship, when I am composing a notification, I can target specific users by searching for a Urban Airship channel_id (device ID):
I sent from Urban Airship the push notification that I am showing above, and I received it successfully. Now I want to do the same thing, send a push notification to a specific device, but now using the Firebase console. The problem is that in Firebase, in the step where I need to specify the target, it only allows me to choose User segment or topic. I was expecting to see a third option: Target specific users (for sending notifications to one or many specific GCM/FCM device token IDs. So my alternative is to add a GCM/FCM device token ID to a topic and then send the push notification to that topic, which is something that I have successfully done before. Nonetheless, that would be a workaround and not the way I would prefer to do this. Is it possible to send push notifications to specific users (by defining the target GCM/FCM device token IDs) from the Firebase console? Thank you.
UPDATE 1: See how the Firebase console (https://console.firebase.google.com/) only shows User segment and Topic as the Target:
First Step:
Second Step:
Third Step:
By design, the notification feature in the console is for sending out broadcast-type events. Sending user-specific alerts would be more of a programmatic operation done through the API.
For sending test messages, there is a console tool for this, explained here.
Note that it is possible to send a notification to one device by subscribing a device to any topic and sending the notification to that topic in the console (keep in mind topics are public and you can't prevent users from signing up to them; fine for testing most likely).
Another alternative is to send a message via HTTP or curl. Perhaps the best resource for this is the quickstart/messaging example.
I am using push notifications in meteor app for ios and android. It works fine if the users to whom I am sending the notifications are connected to the internet. The problem that if I send a notification to an offline user and when he connects to the internet after few hours, the notification is not delivered. Any insights as how to overcome this.
Following is my code to send notifications :
Push.send({
from: '1234',
title: title,
text: text,
notId : nId,
gcm: {
title: title,
style: 'inbox',
},
query: {
userId: {
$in: userIds
},
}
});
I couldn't find TTL(Time To Live) option in the gitRepo you provided.. usually there is an option for that.. You can read more about that here concept options
Update
New Link
Lifetime of a message When an app server posts a message to FCM and
receives a message ID back, it does not mean that the message was
already delivered to the device. Rather, it means that it was accepted
for delivery. What happens to the message after it is accepted depends
on many factors.
In the best-case scenario, if the device is connected to FCM, the
screen is on and there are no throttling restrictions, the message is
delivered right away.
If the device is connected but in Doze, a low priority message is
stored by FCM until the device is out of Doze. And that's where the
collapse_key flag plays a role: if there is already a message with the
same collapse key (and registration token) stored and waiting for
delivery, the old message is discarded and the new message takes its
place (that is, the old message is collapsed by the new one). However,
if the collapse key is not set, both the new and old messages are
stored for future delivery.
If the device is not connected to FCM, the message is stored until a
connection is established (again respecting the collapse key rules).
When a connection is established, FCM delivers all pending messages to
the device. If the device never gets connected again (for instance, if
it was factory reset), the message eventually times out and is
discarded from FCM storage. The default timeout is four weeks, unless
the time_to_live flag is set.
To get more insight into the delivery of a message:
For Android and iOS: See the FCM reporting dashboard, which records
the number of messages sent and opened on iOS and Android devices,
along with data for "impressions" (notifications seen by users) for
Android apps.
For Android: If you'd like to be notified when the application
successfully receives a message, you can use
delivery_receipt_requested functionality following the guidelines.
This requires you to setup an XMPP server.
For Android, iOS and Web: You can use InstanceID APIs to check the
most recent date that the device you're targeting through the FCM
registration token has established a connection with FCM.
I had read that not to add notification node in the body of the request, and I tried to send without notification node but it seems that the message not received but when I add the notification node it work well
So, what is the difference notification node and data node in Firebase cloud messaging?
{
"to": "/topics/some_topic",
"data": {
"key_1" : "some_value",
"key_2" : "some_value"
},
"notification":{
"body" : "some_message"
}
}
Is there any link to doc I can read ?
The data node is used for sending notification if the application is on the background/foreground and in some phones if it is also killed.
The notification node is used for sending notification if the application is on the foreground. If it is in the background you wont receive the notification.
The best option I found is to use data node alone.
Also it is explained very well in this link: The FCM messages types
Notification node (a.k.a Notification messages)
When sent, this will receive a notification on the device, regardless of whether the app is in foreground/background/terminated state, but the notification will be shown only when the app is not in foreground. You can bundle a data payload with this of upto 4 KB. The upside is that you do not have to worry about generating a notification every time, the libraries take care of that. The downside is that there is no way not to show the notifications, for eg when you want to do something silently.
Data node (a.k.a Data messages)
When sent, this will fire up all the same callbacks as a normal notification node would, the only difference being that a notification won't pop up on its own, you are the master of your own callback! This is useful for when you want to do something silently, like refresh the cache, update the database etc. As a data node will have the data payload and will fire up a callback at any state of the app, you can generate a notification on your own and fire it up. This gives you the freedom to design your notification however you want!
I am looking for some input on what might be a solution to the case where I will have to send a push notification to many devices at once. The notification will retain the same message for every device that it is sent to. I have looked into AWS SNS, but I was unable to find anything on the idea of subscribing multiple devices to one topic, or if this is a possibility? If not, another solution to this problem would be to loop through n device tokens which might not be the most efficient way of doing so.
If you have any other recommendations, they would be greatly appreciated!
Thanks in advance.
Firebase Cloud Messaging provides the feature that you are looking for
under the name of Topic Messaging
https://firebase.google.com/docs/cloud-messaging/android/topic-messaging
In you app you can subscribe for a topic:
Android: FirebaseMessaging.getInstance().subscribeToTopic("news");
iOS: [[FIRMessaging messaging] subscribeToTopic:#"/topics/news"];
Then your server can send a message to all the devices:
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{
"to": "/topics/foo-bar",
"data": {
"message": "This is a Firebase Cloud Messaging Topic Message!",
}
}