Unsubscribe all users from a topic in FCM - firebase

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.

Related

Firebase Messaging topic unsubscribe : Do we need to unsubscribe explicitly if fcmToken changes

My flow is:
At the login store FCM token in firestore DB and also subscribe to 2 topics for notification
On change/refresh of fcmToken : again store it in DB (replace old fcmToken and keep current) and subscribe again to those 2 topics.
My doubt is do i need to unsubscribe old FCM token from those 2 topics ?
The Firebase Cloud Messaging topic fan-out service, which handles the mapping from a topic to the associated FCM tokens, automatically detects invalid and expired tokens and removes them as needed. So while you can unsubscribe them yourself, it is not required to do so.
The approach FCM takes for this is similar to what you see in to this code sample from one of our repos. You won't need it when you only use topics, but if you ever decide to implement your own token registry you'll want to use that approach to prune that registry.

How to secure Firebase Messaging topics with cloud functions?

Is there a way to secure validate subscriptions to topics? For example, is it possible to limit topic Test to a specific user with ID XXXXX? Is this possible with Cloud Functions?
firebaser here
To be able to subscribe to a topic, you currently need to know two things: the FCM token/instance ID of the app instance, and the path/name of the topic to subscribe to.
Knowing these two allows one to subscribe to the topic from any client. There currently is no public API to limit who can subscribe to what topics. So if you need to guarantee that the message is only delivered to authorized app instances, you should not use topics and instead delivery to each FCM token/instance ID directly from your own (server-side) code.
This request comes along regularly though, so I recommend that you file a feature request to add your vote.

How to use FCM topics for notification in chat app?

I am make chat feature with Flutter and Firestore backend.
Every message is new document in Firestore collection with UID and text field. Chat is 1:1 and random so no know who user will talk to before enter chat. DocID in chat collection are all auto-id.
I have read can use topics to manage send notification. This should be easier than use individual device fcm token.
Anyone know how to implement use topic for this random 1:1 chat app?
You can definitely use a separate topic for each 1:1 conversation, for example with the naming scheme I described here: Best way to manage Chat channels in Firebase. But there are some things to consider which, as Doug already pointed out in his comment, leads most developers to not solely use FCM for their chat apps.
For example: FCM topics are not secured. This means that anyone who finds out the topic ID can subscribe to it, and thus overhear the 1:1 conversation. And while you can generate topics that are hard to guess, you should not rely on not knowing the topic ID as a security mechanism.
Another reason to consider alternatives is that FCM messages are transient: once they are delivered there is no longer any trace of them. With your current Firestore implementation you can query the database to get all messages to show, while with a pure FCM implementation you will have to build your own database (if that is required for your app).
For these reasons most chat apps I know of use a combination of FCM (for push notifications) and an online database (for persistence) as their backend services.
I found Frank's comment really interesting about the "sorted userID composed key". I would probably use that as the chat key in the database (realtime/firestore), however, for the notifications I think I would still use a topic for each user - this way I would be able to avoid notifying the user who posted the message. If that wouldn't be an issue, then just go for just one topic per chatroom.
Also mentioning Frank, I would probably use extra keys in all topic names to make them really difficult to guess. (but add that later so you don't get distracted with non core stuff)
In this answer you have an example of how to post the notifications with a onCreate trigger to a topic (from a functions backend).
In the flutter code, you can use subscribeToTopic from the firebase_messaging plugin to start listening to a topic.
Note: if your app will support user log-off [probably it will :)], then you'll also have to delete the token in the device to avoid receiving notifications from the last logged user.

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 Cloud Messaging - Managing Registration Tokens

I'm looking at implementing messaging between mobile and browser apps using Firebase cloud messaging and i have a few questions, that the docs don't seem to answer.
For being able to receive messages, you need a Registration Token (RT).
Messages can be send to a RT, to a topic or to a device group notification_key.The RT can also be used:
directly
to subscribe to a topic
to add to a device group
The RT can also expire/change.
In my app, I'm maintaining a list of RT per user. Now, when the RT changes:
Do I have to unsubscribe the old token and subscribe the new token to topics?
Do I have to remove the old token and add the new token to device groups?
Is it possible to get information about device groups/topics for a token?
Can I add a token to a device group more than once?
Can I subscribe a token to a topic more than once?
Will multiple subscriptions/additions of the same token result in receiving duplicate messages
Sorry, that's a lot of questions, but I guess, for somebody who has gone through this, it should be pie ;)
Do I have to unsubscribe the old token and subscribe the new token to topics?
AFAIK, you don't have to unsubscribe the old token, since it will be discarded by FCM itself. For the new token, yes, you'll have to subscribe it to the topic you need. The usual thing is done (in Android) by having subscribeToTopic() in onTokenRefreshed().
Do I have to remove the old token and add the new token to device groups?
Yes. You have to handle the mapping/relationships for Device Group Messaging. See my answer here. This is different from topics. The token will be invalidated, but will be kept as part of the list of registration tokens for the corresponding registration key.
It's why there's a possibility to receive a NotRegistred error on one of the tokens if you send to Device Group. :)
Is it possible to get information about device groups/topics for a token?
For Device Group Messaging (same with #2), the developer (you) have to manage these details yourself. For topics, you can use the InstanceID API. Specifically, set details parameter to true:
[optional] boolean details: set this query parameter to true to get available IID token details, including connection information and FCM or GCM topic subscription information (if any) for the device associated with this token. When not specified, defaults to false.
Can I add a token to a device group more than once?
Ahmm. Yes. Do you mean the same token? If so, I haven't tried it yet. Might as well do some checking on the client side before adding.
Can I subscribe a token to a topic more than once?
If you mean re-subscribing, then yes. If you mean duplicate request to subscribe, I think the result would still be a success. No changes in behavior though.
Will multiple subscriptions/additions of the same token result in receiving duplicate messages?
Tested it out. You won't receive duplicate messages for both duplicate topic subscriptions and adding the same token to a device group. It seems that FCM ignores the request to subscribe/add a Registration token if it's already subscribed/added to a device group.

Resources