Firebase Cloud Messaging - Check Existing or Available Topics

Firebase Cloud Messaging - Check existing or available Topics

As already mentioned by @FrankvanPuffelen in the comments section, there is not available API to get a list of Topics you have.

What you could do is keep record of the topics you have created on your server side. So it pretty much depends on your own implementation.

Also, if you are thinking of checking the number of subscribers of a specific topic so you can see which ones are active or not, it's also not possible. See this answer by @ArthurThompson:

No. There is no current way to query the number of subscribers to a topic, you would have to maintain the relationship between token and topics on you app server.

Get all subscribed topics from firebase cloud messaging

I have searched Android API, asked questions for the same on SO but din't find anything. There is nothing in Android API to get all topics of a specific token.

However, you can do it through a GET request

HTTP GET Request

https://iid.googleapis.com/iid/info/<TOKEN>?details=true
Content-Type:application/json
Authorization:key=AAA....i1nM:APA9.....81gTPXCE55....JLPEG0wZobG_ile8lI35JTzHYE5MC..BmDD_Cxj5OxB1Yh....Rs5lo3UwLNL9h-WcocGV....b5bYWNI55kzNsrHK-7GljUDtMn

TOKEN in url : FirebaseInstanceId.getInstance().getToken(senderId, scope);

key : can be found in firebase console: Your project -> settings -> Project settings -> Cloud messaging -> Server Key

Note: Be careful when finding key, dont use web api key its different.

senderId can be found in Settings -> Cloud Messaging -> Sender ID

scope is usually "FCM"

Can a server find out which message topics have subscribers?

No. There is no current way to query the number of subscribers to a topic, you would have to maintain the relationship between token and topics on you app server.

How to know if a Topic creation is submitted to console (Firebase Cloud Messaging)

Found.

It takes few hours to add topic to console, but it's already usable before that, as said.

You cannot know it through Firebase console, but you can do an HTTP Request in order to know if you are subscribed to that particular topic.

HTTP REQUEST

https://iid.googleapis.com/iid/info/<input token here>?details=true

How to take token

FIRInstanceID.instanceID().token()

COMPLETE CODE

(This code let you unsubscribe from all topics)

    func unsubscribe_topics(){


let token = FIRInstanceID.instanceID().token();

let url = URL(string: "https://iid.googleapis.com/iid/info/\(token!)?details=true")

let request = NSMutableURLRequest(url: url! as URL)
request.httpMethod = "POST"


//define the multipart request type


request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("key=<insert server key here>", forHTTPHeaderField: "Authorization")


let session = URLSession.shared


let task = session.dataTask(with: request as URLRequest) {
(data, response, error) in

guard let _:NSData = data as NSData?, let _:URLResponse = response, error == nil else {
print("error")
return
}


let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)

let dati_da_string = dataString?.data(using: String.Encoding.utf8.rawValue)!
let parsedData = try? JSONSerialization.jsonObject(with: dati_da_string!) as! NSDictionary
if let rel = parsedData?["rel"] {
let relaz = rel as! NSDictionary
let topics = relaz["topics"] as! NSDictionary
let topics_registrati = topics.allKeys as! [String]
for i in 0...(topics_registrati.count-1){
FIRMessaging.messaging().unsubscribe(fromTopic: "/topics/\(topics_registrati[i])")
print("Unsubscibed from: \(topics_registrati[i])")
}
}



}

task.resume()

}

How to get all topics list of Firebase through API?

There is no public API to get the list of topics for Firebase Cloud Messaging

How to get client FCM tokens from a FCM topic

There is no public API to retrieve the list of tokens that are subscribed to a topic. If you want such a list, you'll have to maintain it yourself.

Also see:

  • List of clients who have registered for a topic in FCM
  • Count subscribers of a topic in Firebase Cloud Messaging
  • Firebase Cloud Messaging - Check existing or available Topics

Count subscribers of a topic in Firebase Cloud Messaging

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.

Android/Firebase - Check to see if you are subscribed to topic

There is currently no way to check on the client side if they are subscribed to a topic.

The behavior for subscribeToTopic is it would immediately subscribe to the specified topic, if it fails, it would retry on it's own (unless your app was killed). See my answer here.

I think that forcing the onTokenRefresh call just to make sure that subscribeToTopic is too much. You could simply just call it in your initial activity if you want, that way, everytime the app starts, it sends the subscription request.



Related Topics



Leave a reply



Submit