Get all subscribed topics from firebase cloud messaging

AndroidFirebase Cloud-Messaging

Android Problem Overview


Using the new FirebaseMessaging it's easy to un/subscribe to topics via:

FirebaseMessaging.getInstance().subscribeToTopic();
FirebaseMessaging.getInstance().unsubscribeFromTopic();

But is there any way to get all topics the current installation is subscribed to ?

Android Solutions


Solution 1 - Android

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"

Solution 2 - Android

For those that would like to test it with command line CURL follow bellow the syntax that I have used:

#curl --location --request GET 'https://iid.googleapis.com/iid/info/yourFCMDeviceToken?details=true' \
--header 'Authorization: Bearer YourProject_CloudMessaging_ServerKey'

Response:

{
	applicationVersion: '4194309',
	application: 'com.domain.app',
	scope: '*',
	authorizedEntity: '913674269572',
	rel: { topics: { topicName: { addDate: '2020-08-19' } } },
	appSigner: 'ae7cbSomeHash23jsdfff34ac7ffd',
	platform: 'ANDROID'
} 

Solution 3 - Android

Never tried programmatically.

But you can do it by goto firebase console->your project->cloud messaging->new notification->target-> click on topic tab next to user segment just click on Message topic text box you will get list of subscribed topics

enter image description here

Solution 4 - Android

This topic is still relevant, still no API for this in the iOS SDK.

If your goal is to prevent a user from subscribing multiple times to the same topic and therefore getting notified multiple times for, say, a single comment in a group, my solution is a simple local cache using UserDefaults.

Pretty straightforward:

    func subscribeTo(topic: String){
        // first check that the user isn't already
        // subscribed, or they get multiple notifications
        let localFlag = UserDefaults.standard.bool(forKey: topic)
        if localFlag == true {
            print("user already subscribed to topic: \(topic)")
            return
        }
        print("attempting to subscribe user to topic: \(topic)")
        // Subscribe to comet chat push notifications through Firebase
        Messaging.messaging().subscribe(toTopic: topic) { error in
            if error == nil {
                print("subscribed CometChat user to topic: \(topic)")
                // set local flag as "already subbed"
                UserDefaults.standard.setValue(true, forKey: topic)
                return
            }
            print("attempt to subscribe CometChat user to topic \(topic) failed: \(error?.localizedDescription ?? "(no error provided)")")
        }
    }

The flow of my application logs a user in, and then automatically gets a list of topics associated with the user and auto-subscribes to the topic with each launch.

The reason for this is a high degree of assurance that the user is getting notified.

The flow: User launches app-> topics retrieved-> iterate & pass topic to subscribe func -> block if topic == true-> pass through if topic != true

And then of course we assign nil to the local bool at the topic key upon unsubscribe.

Unsubscribes are always successful without such blocking / checking, because it's better UX to be more conservative when a user does NOT want notifications.

Cheers.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndre ClassenView Question on Stackoverflow
Solution 1 - AndroidHisham MuneerView Answer on Stackoverflow
Solution 2 - AndroidCassio SeffrinView Answer on Stackoverflow
Solution 3 - AndroidKhalid LakhaniView Answer on Stackoverflow
Solution 4 - AndroidDan BurkhardtView Answer on Stackoverflow