Firebase messaging subscribe to topics after token refresh and multiple devices sync - firebase

From my testing and understanding,
I noticed that when a user subscribes to a topic using Device A, and logs in on Device B.
Device B will not receive that topic's notification unless the user subscribes to that topic from Device B as well. How can i have both devices receive the notification when only one device subscribes?
When using the FirebaseMessaging.instance.onTokenRefresh.listen() stream, and the users token changes, how can i subscribe his new token to his previously subscribed topics? (Note: I store them in the database).

Related

Does firebase cloud messaging allow targeting of push notifications to a specific device/user etc

I am trying out firebase cloud messaging (React-native-firebase) and it seems that I am only able to push notifications to all android/ios users, not specific users. Is there a way to target a specific user. Like if a user invites another user only send notification to that one specific user?
The flow can be this one:
User allow notifications on their device(s)
Grab the generated token and store it in the user document inside a tokens array.
(If the same user connects to your app in another device, add the new token in the tokens array)
If you want to target a specific user, get his UID and send a notification to their devices.
You can also subscribe a user to a topic and send a notification to users subscribed to that topic.
two way:
init
your client-side subscribe the topic.
subscribeToTopic("weather")
your client-side report the user property
setUserProperty("name","TOM");
Firebase Cloud Message Console
crate a notification item like this way
The flow can be this as follows:
User allow notifications on their device(s)
Grab the generated token and store it in the user document inside a tokens array.
(If the same user connects to your app in another device, add the new token in the tokens array)
If you want to target a specific user, get his UID and send a notification to their devices.
You can also subscribe a user to a topic and send a notification to users subscribed to that topic.

Firebase Cloud Functions - Send Notification user when they are online again?

I'm using fcm to send user notification and its triggedred by writing data to firebase database. But when user is offline notification send but never received and user cannot see it after they back online. How to send notification in all conditions.
Example: If user online send message normally but if user offline wait until user online and send them. How can I do this?
According to documents firebase cloud messaging already support what i want.
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.
So FCM service wait until client device connected again.And sen notification when cliend connected. But if user hasnt connected for four weeks message deletes itself and never sent.
https://firebase.google.com/docs/cloud-messaging/concept-options#ttl

Sending Firebase message to topic to same user on multiple devices

I have an app that lets users authenticate with phone auth, and subscribe to topics to get notifications. If a user authenticates on two devices like an ipad and iphone, and subscribes to the same topic on both, when I send a message to that topic, it's only received on one of the devices. Is this correct and by design? If the user has an iPhone in their pocket and an iPad at home, the notifications may only get displayed on the iPad at home, and they miss the notification.
Yes, this is correct. When you create a topic and lets say a user subscribe to that topic, then that device will be associated with that topic.
If the user opens his account in another device and you send a notification, it won't be received on that device. So, it will only be received on the device the user subscribed to that topic. If the user is subscribed to a topic on multiple devices, all devices will receive the notification.
Also, if only one user is subscribed to that topic and he unsubscribe then it is no longer a topic.

Firebase Cloud Messaging to send notification for set of users

I'm developing an app using Firebase, in the app there are groups and for groups there are multiple members. I want to send notifications to members in a particular group based on one of the users activity. As an example in a chat app, notification to others informing there is a new message.
I referred this blog post. As it seems best way to do this is Firebase Topic Messaging, all members can be subscribed to the group id as a topic.
My matter is how to avoid receiving notification to the member who's action causes to the notification.
I tried Firebase message sending to multiple topics but it only allows for two logical operations (&&, ||)
Firebase Cloud Messaging has two types of messages: notification messages and data messages.
The default behavior of Firebase is that notification messages will automatically be displayed if the app is in the background or not running. This behavior cannot be modified. If the app is in the foreground, the notification message will not be automatically shown, it will instead be delivered to your app's code.
Data messages on the other hand will always be delivered to your app's code.
Since the user sending the message will typically have the app open, the default behavior will automatically ensure the notification is not shown on the sending user's device.
But in the blog post you refer to there is a race condition for this. If the user closes the app between the time they send the notification to the server and the time the server sends out the notification, the notification may show on their device too.
If this is a concern for your app, you should not use a notification message. Use a data message instead and include enough information in the message to detect which user has sent it (e.g. the user's uid).

Worklight send push notifications without username

I want to create an application which have 2 modes: non-logged-in and logged-in (having username).
I want to send push notifications to devices in both modes. The non-logged-in devices should receive notifications such as general announcements, new events, etc. The logged-in mode devices will also receive same notifications and few more.
Now what I want to do is to subscribe the device to receive notifications for non-logged-in mode then once the user logs in to his account from that device I want to unsubscribe the previous non-logged-in notifications.
I've read the answer to this question : Worklight: Push notification without User ID I think that I can play with onUserSubscribe callback when registering the user and unsubscribe the userId of the persistent cookie.
Is this a good idea ? Is there another suggestion ?
My thinking is that you could register an app to 2 event sources.
The first event source will handle the general notifications that an app should receive, whether a user is logged-in or not.
The second event source will handle the notifications for logged-in users.
When the app launches, subscribe to the first event source.
When the user decides to log-in, subscribe to the second event.
You should allow the user to unsubscribe from both if s/he so chooses, as well.
Additional information:
IBM Worklight 6.1 Information Center: search for "push notifications"
Push Notifications training module
So what you're trying to achieve is push notifications broadcast (send to all users). This is something that you'll be able to do with upcoming Worklight 6.2 release. You will not have to subscribe users according to username, but will be able to specify tags during subscription. And after that you'll be able to send push notifications according to this tags.

Resources