Firebase Notification by topic - firebase

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.

Related

Flutter push notification using FCM by topic

hello I'm new to flutter and trying to get push notification in flutter , I have successfully subscribed the user to a topic and sent a notification through the firebase console by topic , what I'm trying to do is to have a button for admins in the app and when they post for a particular topic the users with that topic get the notification I have heard about cloud functions but I don't know exactly where to start ! i hope I made my point ! I have seen some videos that talks about type script but is there is any workaround this ?
The Firebase documentation names notifying the user when something interesting happens as an example use-case. From there:
Developers can use Cloud Functions to keep users engaged and up to date with relevant information about an app. Consider, for example, an app that allows users to follow one another's activities in the app. Each time a user adds themselves as a follower of another user, a write occurs in the Realtime Database. Then this write event could trigger a function to create Firebase Cloud Messaging (FCM) notifications to let the appropriate users know that they have gained new followers.
The function triggers on writes to the Realtime Database path where followers are stored.
The function composes a message to send via FCM.
FCM sends the notification message to the user's device.
To review working code, see Send FCM notifications.

Registering new device tokens to topics to replace previous device token

My app users are subscribed to a variety of cloud messaging topics. Occasionally a users device token/ID will change. I need to lookup the old token, grab all the topics the user was subscribed to - subscribe the users new token to all the same topics and then remove the old device tokens from those groups.
However, I am noticing that as soon as the user gets a new device token it seems they are instantly removed from the previous topics. Thus I am not able to retrieve all the subscribed topics with the old token/ID - and if I can't do that, it seems I have no way to subscribe the users new token to all the appropriate topics because I am unable to find out what the topics were.
Is there some trick or method that I need to do to deal with this issue?

Unsubscribe all users from a topic in FCM

Is there a way to unsubscribe all users from a topic in Firebase Cloud messaging without knowing the tokens? Basically, "delete" the topic?
It would be really really painful to do this manually since it should happen with a document change trigger, meaning the function would somehow have to know all the user tokens. So it's either a function/set of functions for "get all tokens for topic, then unsubscribe all of them", or "delete topic". Are there any solutions to this?
There is no API to unsubscribe all tokens from a specific topic. There also is no API to get a list of tokens for a topic. See How to get client FCM tokens from a FCM topic
Topics are automatically created and deleted by Firebase Cloud Messaging. A topic is created when you first subscribe a token to it, or send a message to it. And it's essentially deleted when you remove the last token from it.

Flutter: Cloud Message Not Delivered to Topic Subscriber

I call _firebaseMessaging.subscibeToTopic('topic-title'); to subscribe user to a topic on button click.
Some minutes after, the new topic showed at firebase cloud message console and i sent a message to the topic but I didn't received it. I don't understand why this is not working. I think there should be a option to add user's token to the subscribeToTopic('topic-title', token) or is the token automatically detected? Because am afraid the topic was only created with no token added.
Please i need help.
You don't need user token for topic notification
firebaseMessaging.subscribeToTopic('newJobs'); //you are doing it correctly.
Now I think all you need is to minimize your running app while sending the notification because notification will not appear, if your app is on the resume state.

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).

Resources