Easy way to unsubscribe from all topics subscribed to GCM (iOS device)

I am trying to send notifications via the themes system on an iOS device with the new Google Cloud Messaging API developed for the iOS device.

I have the correct certificates, so I can receive notifications from the created topic. My code to subscribe to the topic:

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 requires a theme name. Is there a way to get all the topics on which my application may have subscribed to unregistered them before subscribing?

+8
ios google-cloud-messaging
source share
4 answers

It is not possible to get the list of topics that your application is subscribed to from Google Cloud Messaging.

You need to track the list and save it in your application (hard-coded, saved in settings, database, file, etc.) or on your server.

When you decide to allow the user to unsubscribe, extract the list of topics from the place where you saved it and pass it to unsubscribeWithToken: token: subject: parameters: handler, as indicated in the Implementation of the Message Subject page

+7
source share

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

+6
source share

If you want to unsubscribe from all topics, just do:

 GGLInstanceID *iid = [GGLInstanceID sharedInstance]; GGLInstanceIDDeleteHandler deleteHandler = ^void(NSError *error) { if (error) { // failed to delete the identity for the app // do an exponential backoff and retry again. } else { // try to get a new ID dispatch_async(dispatch_get_main_queue(), ^{ GGLInstanceIDHandler handler = ^void(NSString *identity, NSError *error) { if (error) { // failed to get the identity for the app // handle error } else { NSString *instanceID = identity; // handle InstanceID for the app } } [iid getIDWithHandler:handler]; }); } } [iid deleteIDWithHandler:deleteHandler]; 

Additional Information

Do not forget to update your TOKEN!

0
source share

If you have a registration token, you can get the topics that the device is subscribed to using https://iid.googleapis.com/iid/info/IID_TOKEN (with an authorization key in the header). Where IID_TOKEN is the registration token.

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

0
source share

All Articles