What I want to do with my app is this: users can select different interests and as soon as there are more than ~5 people interested in the same thing, I want to send them a notification.
I thought Firebase + Topics would be a good choice for this.
But how do I find out how many people have subscribed to a topic?
There is no available API to check how many subscribers a topic has. (see my answer here)
You'll have to implement the mapping on your server-side, saving the name of the topics and adding in the list of subscribers. Upon subscription, add the new subscriber, check the count (see if it's within your preferred number), then trigger a notification.
U can use this to get number of subscribers :
Go to the Firebase Console "https://console.firebase.google.com/"
Under Grow, click Cloud Messaging.
Open up your developer tools.
Click New Notification and type some title and message
On Target Tab , click on Topic
In the network tab in developer tool you will see a request to an url similar to this one
https://gcmcontextualcampaign-pa.clients6.google.com/v1/4/projects/${projectId}/topics?prefix=%2Ftopics%2F&key=${key}
Copy as a curl. Use that to get subscribers list
Reference: https://gist.github.com/timrijckaert/218be90541a7285e21455fafa2962dea
There is no direct way of doing this. Your only two options are:
Using stored deviceIDs in your own DB, manually script to retrieve each users subscriptions.
Already track in your own DB each users deviceIDs AND every topic they subscribe to.
There are so many problems with both of these methods though. It's frustrating topic managmenet doesn't already exist in FCM Console. I wrote Google a feature request for FCM Cloud Notification Topic Management and this is the response I got:
We're aware that many developers, such as yourself, would like to have
this topic management. There's actually an existing feature request
regarding this. However, I can't provide any details or timelines as
to when this will be available.
Related
I am trying to develop the Messenger App like Whatsapp using Firebase.
I'm already done the code for device to device notifications and now I am trying to send the notifications to specified group.
Please provide the example code for the Group Notifications.
There are a few ways to send messages to groups of users:
Send the message to a topic that all users subscribe to. This is the simplest scenario, since you don't need to keep a list of tokens. But you have less control over the actual people that receive the message, as anyone who knows a topic can subscribe to it.
Keep a list of device tokens for each group in your app server, and then send to each individual device in the group.
You can also use the previous version of the API to send to batches of up to 500 tokens at a time. This sort of multicast delivery is not yet possible in the new API.
For an example of this, see the documentation of Cloud Functions and the sample repo.
Send to a device group with the legacy API. I'd only recommend this approach if the devices are actually owned by one user, which is the case that device groups are meant for.
Try this link
Don't forget this line Authentication:
key=<Your_API_Key>
The word (key) is important
I am already obtaining successfully the Firebase Cloud Messaging (FCM) device token by using FirebaseInstanceId.getInstance().getToken(). I used the following code to subscribe the client app to a topic:
String topic = "toronto";
FirebaseMessaging.getInstance().subscribeToTopic(topic);
I assume everything is correct but in order to confirm, I would like to have a Graphical User Interface (GUI) provided by FCM to manage, monitor and see all of the devices that are subscribed to specific topics. With the code I used, I would expect to see the topic "toronto", and at least one device subscribed to that topic, for example by showing the FCM device token.
I was reading at https://developers.google.com/instance-id/reference/server#get_information_about_app_instances how it is possible to get information about app instances to find out details about a client app instance's subscriptions, including each topic name and subscribe date, but does FCM provide a GUI to see that?
UPDATE 1:
I was able to subscribe devices to topics and unsubscribe devices from topics. But when I want to confirm/monitor that a device was subscribed successfully, I am using this for example (for privacy I changed values of keys and tokens):
C:\curl>curl -X GET -k --header "Authorization: key=[My key]" "https://iid.googleapis.com/iid/info/[My device token]?details=true"
{"applicationVersion":"22","connectDate":"2017-12-05","attestStatus":"NOT_ROOTED","application":"com.[My app]","scope":"*","authorizedEntity":"[My app ID]","rel":{"topics":{"San-salvador":{"addDate":"2017-12-05"}}},"connectionType":"WIFI","appSigner":"[My signature]","platform":"ANDROID"}
C:\curl>
I was expecting Firebase Cloud Messaging to provide a Graphical User Interface with a dashboard, charts and reports to see the topics that have been created and the list and number of devices subscribed to each of the topics, something similar to the Google Analytics reports, maybe even with maps to see where devices are subscribing from or anything visual that can help to visualize and monitor topic subscriptions. Instead, I am having to do everything with cURL with code similar to what I am showing in this UPDATE 1. I am surprised Firebase Cloud Messaging does not provide a GUI, since the tool comes from Google and they could easily provide a dashboard or something similar to Google Analytics.
FCM currently doesn't have a GUI that displays the list/count of topics you have or the subscribers (count/registration tokens) the topic has.
At most, you could use the Instance ID API (the one from your post) to check a single registration token to which topics it is subscribed to. However, do note that the Instance ID API was meant to be used on your Server side.
Other than that, you will have to implement your own mapping with the topics (which tokens are subscribed to it).
Kinda similar/possibly helpful posts:
Firebase Cloud Messaging - Check existing or available Topics
Count subscribers of a topic in Firebase Cloud Messaging
Android/Firebase - Check to see if you are subscribed to topic
Firebase Cloud Messaging - Managing Registration Tokens
Let's say I have an Android app with 100 users that I don't know personally. Is it right to make each one of them subscribe to topics like FirebaseMessaging.getInstance().subscribeToTopic("<company_id>_<user_id>"); so I can address them and send notifications for one or two specifically or there is a better way to do that?
If you intend to send a message to specific users only, you could simply go ahead and use to or registration_ids when targeting the specific users. registration_ids has a limit of 1000 tokens per request.
As per the topics, it was designed to easily send messages to its subscribers. Depending on your use-case, it could be fine to subscribe them. However, you should still keep the registration tokens for each user in case you need to send specific messages.
It all depends on your goal.
Anyone can subscribe to a topic, so you should only use those for sending messages targeted-but-public messages. While you can counter this a bit by making the topic names hard to guess, the inherent behavior of topics is that you lose control over precisely what devices are targeted in return for having to write less code.
If you send to a token in your own code, you determine precisely who receives the message. But you will have to run the code that maps the message to the tokens yourself.
I think the answers are pretty clear, I would only like to add that, this method of subscription it can be easily combined with the Firebase web console. If you want to quickly send a message to any topic, the topic is created on registration, so you only have to know the topic name.
In the Firebase Web Console, you can find the target, an application, a token for a device or a topic. If you choose a topic, just write the topic name. There is an error which always says there is no topic, disregard it, and click send, the push will be sent and the subscribers will get their notifications.
Is there a way to obtain the list of clients who have registered for a particular topic in Firebase Cloud Messaging (FCM)?
I am planning to subscribe each client devices' location info as a topic. Later, when I want to get all devices in an area, I can just get a list of clients in that specific area. For example, clients in a sublocality 'Sharafiyah' can be listed under
topics/SA/Jeddah/Sharafiyah
For now, there is no provided solution to get the list of clients have registered to the specific topic in FCM. You have to create your own logic on the server to achieve your requirement to get list of clients registered for a topic.
But according to the below section
Get information about app instances
in the link Instance Id from developers site explains how to get list of topics are subscribed by a clients Instance ID.
Hope this will help you.
There is no way easy way to do this. The ONLY way is to already know all your users deviceIDs and scripting the retrieval of each users subscriptions there is no other way of doing this. Or from the beginning, as each user subscribes to a topic you save that data in your backend DB. It is a real pain. I wrote google a feature request regarding FCM Cloud Messaging Topic Management and this is the response I got from google:
We're aware that many developers, such as yourself, would like to have
this topic management. There's actually an existing feature request
regarding this. However, I can't provide any details or timelines as
to when this will be available.
In my settings screen, I provide users notifications switchers each topics. So I would like to make sure with the status when I subscribe or unsubscribe to turn-on or turn-off the switchers UI. Actually I found where can I get topics subscribed each users from this URL https://developers.google.com/instance-id/reference/server.
Is there any easier way to check them? (e.g. Get information about app)
Unfortunately, there is no direct API available as of the moment to check the subscriptions of a specific user on the Client Side. You'll only be able to check it using the Instance ID API.
However, perhaps you can implement something in your App Server to have the list of subscriptions the user has, and have it reflect in your Client App.
Cloud Messaging version 17.0.0 almost adds this functionality. The subscribe methods now return a Task object that will complete when the operation is successful.
However, it doesn't really help with seeing what users are currently subscribed to.
https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/FirebaseMessaging.html#subscribeToTopic(java.lang.String)