How to delete a topic from Firebase Console? (FCM) - android

Does anyone know how to delete a client topic from the Firebase Console?

There is no way to delete a topic directly. Not even if you have an app server. That's what I can say after going through the GCM and FCM documents.
The way I understand it, a topic is created when there is at least 1 subscriber and it also gets automatically deleted when there are no more subscribers. So I think the way to do this is to unsubscribe all the subscribers of that topic.
You can check more details on unsubscribing in the Managing Topic Subscriptions on the Server docs (both sites below pretty much says the same thing, but I think it's appropriate to provide a link to both of them):
https://firebase.google.com/docs/cloud-messaging/topic-messaging#managing_topic_subscriptions_from_the_server
https://developers.google.com/cloud-messaging/topic-messaging#managing_topic_subscriptions_from_the_server
Hope this helps somehow. Cheers! :)

Related

How to replace the subscribed topic for a Device/User in FCM

Currently I've a requirement in which what I have to do is to subscribe different topic for different type of users.
The use case is if multiple users logs in from single device what is the method to subscribe and unsubscribe the topic according to the Logged In User's Role.
A quick solution comes up is to unsubscribe all topics and then subscribe the appropriate one.
Suppose user3 logged-in into the application. Unsubscribe all topics.
FirebaseMessaging.getInstance().unsubscribeFromTopic("Topic1");
FirebaseMessaging.getInstance().unsubscribeFromTopic("Topic2");
FirebaseMessaging.getInstance().unsubscribeFromTopic("Topic3");
Then subscribe the appropriate one.
FirebaseMessaging.getInstance().subscribeToTopic("Topic3");
I am not sure this is the best solution for this problem. Anybody has a better approach? Please help me. Thanks in advance
Your approach is correct, unsubscribe when user logs out and subscribe again when new user logs in.

List of clients who have registered for a topic in FCM

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.

Count subscribers of a topic in Firebase Cloud Messaging

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.

Topics on Firebase Cloud Messaging?

Is there any constraints about number of different topics for Firebase Cloud Messaging in one app?
Nope. Seeing that FCM has GCM as its core, there is no limit in the number of Topics for any app. There used to be a 1 million limit, but it was removed. You can refer to this Google Developers Blog for that.
Also, when creating a Topic in FCM, it could take up to 24 hours for it to be show up in the Firebase Console, as per this post. Cheers! :D
No I don't think so!
Based on the publish/subscribe model, topic messaging supports unlimited subscriptions for each app. You compose topic messages as needed, and Firebase handles message routing and delivering the message reliably to the right devices. link
Seems like you can create topics when you need them!

Sending to GCM topics

Probably a GCM newbie question...
I more or less understand how to subscribe to topics and how to send messages to topics. But I was wondering : how does a topic gets created ?
From the docs I read, I guess a topic exists when at least one app subscribes to it, correct ? (As far as I could see there's no specific api to 'create' a topic).
I also noticed that it should be possible to register apps to a topic not from the app itself, but from an app server (by sending a HTTP POST message to a specific URL). Does this work the same way ?
E.g. if the topic doesn't exist when subscribing, it will be 'created'?
As far as I can tell, yes, whatever topic name a client app uses when subscribing, will get created. Even if it is "sdfgklfhjashfgkjas" and purely accidental.
I have seen no mention of deleting topics. I guess unused topics don't really cause Google any problems; they don't take up much space.
On the server side, you can force an app instance to subscribe to a topic by passing the token in a http POST as mentioned in the GCM documentation. Ditto for unsubscribing from a topic.
Bear in mind there are limits on topics which could cause problems for popular apps.
How does a topic gets created?
There is no detailed explanation in the documentation on how topics are created but according to the documentation.
An app can subscribe to different topics defined by the developer. The app server can then send messages to the subscribed devices without having to maintain topic-subscribers mapping. Topics do not need to be explicitly created before subscribing or publishing—they are automatically created when publishing or subscribing.
From the docs I read, I guess a topic exists when at least one app subscribes to it, correct ?
I think this is the case because in order to create a topic you will have an app subscribe to it.
I also noticed that it should be possible to register apps to a topic not from the app itself, but from an app server (by sending a HTTP POST message to a specific URL). Does this work the same way?
Yes your app can subscribe to other topics not necessarily related to your app as long as you pass the GCM registration token and topic name. See Subscribe to a topic.

Categories

Resources