FCM sends an event via onNewToken() whenever an app is installed in a device. Do we have any similar methods available to capture the event for topic subscription or unsubscription, when a device is subscribed to a topic (or) unsubscribed from a topic?
Looking for a way to store the topic subscription details along with the deviceToken for every topic created for an app.
I could not see any such methods similar to onNewToken() for topic subscription events.
Since the token can be refreshed by the server outside of the application's control, the server notifies the client when the topic changes.
Since your application itself subscribes to a topic, you already know when it happens and can track that yourself. So there is no server-to-client notification for when a client subscribes to a topic.
Related
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.
Is there a way to subscribe a existing topic to new topic in Firebase Cloud Messaging, so that all the registered clients of old topic will be registered to new topic.
Example: If I have a FCM topic name A and I create a new FCM topic B. Can I add topic A to topic B?
No, only client apps can determine which topics they want to subscribe to in order to receive messages. You can't route messages going to a topic through another topic. What you would have to do instead is program the server side code that sends the message to send to multiple topics as needed.
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.
Scenario: Send notification to topic "X" with expire time 4 weeks.
What happens when:
Topic does not exist, no one subscribed to it. Then after one hour,
someone subscribe to it. Will this device get this notification?
Topic exists or existed (had one subscriber but he unscusribed), now he is subscribing again after an hour of sending. Will it get it?
One or more devices are subscribing to topic X. All of them got it. One new device now subscribe to it, after as always, an hour from sending. Will it get it?
Something is telling me "No" to all of those answers, but have problem with finding that answer in fcm documenation.
If I guessed right, what would be solution to get this previous notifications? Let's say for business it is important, and notification lives only for an hour, but in between he could change subscription and after getting back, he would like to get that notification.
FCM Topics are auto-created when you send a message to it, or when somebody subscribes to them.
The device only receives messages that were sent to the topic after it subscribes. It does not receive messages that were sent to the topic when it isn't subscribed.
Since in all three questions the message is sent to the topic when the device isn't subscribed, it will not receive those messages.
For this type of scenario you'll want to use a persistent database, instead of a transient message passing mechanism. E.g. you can store the messages for each topic in a database, and grant the device access to that database when it subscribes to the topic.
Yesterday Google has announced a new set of tools for Firebase, one of them was Notifications the ability to send notifications from server to devices which are using my app.
But can we now notify users when they receive a new message?
And if not, is there a way around to achieve this?
Looking at the documentation, this doesn't seem currently possible automatically. Here is a possible way to accomplish it "manually" with another server:
Subscribe a user to it's own user ID
In android
FirebaseMessaging.getInstance().subscribeToTopic("InsertUserIDHere");
In IOS
[[FIRMessaging messaging] subscribeToTopic:#"/topics/InsertUserIDHere"];
Setup an outside server that checks every sent messages. When a message is sent, the server should create a notification that includes the recipient's user ID as the topic.
Look here for more info.
You can send messages to group of users (targeting a specific "topic") or to a single device (targeting a Firebase Cloud Messaging token).
To subscribe a device to a topic use:
FirebaseMessaging.getInstance().subscribeToTopic("topicName");
To obtain the device token use (*1) :
FirebaseInstanceId.getInstance().getToken();
Then you can use the Firebase Notificaitons web console, or the FCM server API if you want to send messages from your server.
See: https://firebase.google.com/docs/cloud-messaging/downstream#sending_topic_messages_from_the_server
Notes:
[1] getToken() can return null if the token is not yet available.
You can use the callback onTokenRefresh() to be notified when the token is available and when the token is rotated.
See: https://firebase.google.com/docs/cloud-messaging/android/client#sample-register