Sending FCM Push Notification to topic but preventing duplicate notification - push-notification

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"
})

Related

How to send a push notification from the Firebase console to a specific GCM/FCM device token ID

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.

Firebase Notification by topic

I am new to firebase. I want to send notification to my list of followers(group of members like instagram).
I created a unique topic for each user and all the followers of a user will subscribe to the topic. when i send a notification to the topic, notification not delivered to all followers consistently.
When User clicks follow button i am subscribing to the topic by
FirebaseMessaging.getInstance().subscribeToTopic(FollowerUserID);
by clicking unfollow i am unsubscribing the topic
FirebaseMessaging.getInstance().unsubscribeFromTopic(FollowerUserID);
I have used Firebase cloud functions for pushing notification to the Topic.
If Firebase Token changed for a user whether we need subscribe the topic again?
I don't know how the subscribe/unsubscribe model works. will anyone explain how its works ?
kindly help me get out of this problem.
Suggest me the best way to send notifications to group of peoples(Followers).
First thing first.
If a firebase user token changes and that token was subscribed to a cloud messaging notification then he will no longer receive any notifications, also if he was subscribed to a topic and that topic changes he won't get any notifications from the new topic
For your business logic, I'd make a cloud messaging topic for each user and whenever someone follows that user I will subscribe that user token to the topic of the followed user.
say user X followed user Y, and user Z followed user X. both Y and Z will subscribe to topic X.
check this link
https://firebase.google.com/docs/cloud-messaging/admin/manage-topic-subscriptions
Firebase Cloud Messaging topics subscribe based on Instance ID, not FCM Token. An Instance ID uniquely identifies an app device instance and does not change unless the user uninstalls the app. If an app instance is subscribed to a topic, it will remain subscribed to it. While FCM Tokens periodically refresh, this does not affect topic subscriptions. There is no need to resubscribe users every time a token refreshes. See the guide for some useful information. You can also see the reference indicating how subscribeToTopic works with the Instance ID.

Firebase - Sending FCM Post to a single device in a specific topic

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.

what is the difference between notification node and data node in 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!

Deleting the topic immediately after sending a push notification

We are planning using Amazon SNS to send push notification to our users. I am wondering what happens if I do the following programmatically (not important but with PHP SDK)
Create a topic on the fly
Subscribe 10000000 users
Send a push notification to this topic
Delete the topic immediately after sending the push notification
Will all subscribers receive the push notification?
Don't do this. Nothing in the docs say that SNS snapshots the topic when you publish, so don't rely on that behavior even if it seems to work.

Resources