Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to unsubscribe for all topic subscribed with GCM (iOS device)

I am trying to push notifications through topic system in a iOS device with the new API of Google Cloud Messaging designed for iOS device.

I have the right certificates so I can receive notifications from a topic created. My code to subscribe to a topic is the following :

if (_registrationToken && _connectedToGCM) {
    [[GCMPubSub sharedInstance] subscribeWithToken:_registrationToken
                                             topic:topicToSubscribe
                                           options:nil
                                           handler:^(NSError *error) {
                                               if (error) {
                                                 //handle error here
                                               } else {
                                                   self.subscribedToTopic = true;
                                               }
                                           }];
}

I know the equivalent function to unsubscribe but this function need a topic name. Is there a way to retrieve all topics where my app is possibly subscribed to unregistered them before subscribing ?

like image 986
Meitneshi Avatar asked Jun 11 '15 13:06

Meitneshi


3 Answers

There is no way to retrieve the list of topics that your app is subscribed to from the Google Cloud Messaging service.

You have to keep track of the list and persist it on your app (hard coded, stored in preferences, database, file, etc.) or your server.

When you decide to let the user unsubscribe, retrieve the list of topics from where you stored it and pass it to unsubscribeWithToken:token:topic:options:handler as mentioned on the Implementing Topic Messaging page

like image 124
Alex Bitek Avatar answered Oct 19 '22 08:10

Alex Bitek


If you have the registration token it's possible to get the topics the device is subscribed to by using https://iid.googleapis.com/iid/info/IID_TOKEN (with authorization key in the header). Where IID_TOKEN is the registration token.

Find more info at https://developers.google.com/instance-id/reference/server#get_information_about_app_instances.

like image 30
finstas Avatar answered Oct 19 '22 07:10

finstas


Alternatively, when receiving messages you can check who is the message 'from'. If it is from a topic you are no longer interested in, you can unsubscribe instead of processing the message.

like image 42
georgi Avatar answered Oct 19 '22 07:10

georgi