轻松取消订阅GCM订阅的所有主题(iOS设备)

时间:2015-06-11 13:43:43

标签: ios google-cloud-messaging

我正在尝试使用专为iOS设备设计的新版Google Cloud Messaging API,通过iOS设备中的主题系统推送通知。

我拥有正确的证书,因此我可以接收来自所创建主题的通知。我订阅主题的代码如下:

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

我知道取消订阅的等效功能,但此功能需要主题名称。 有没有办法在订阅之前检索我的应用可能已订阅未注册的所有主题?

4 个答案:

答案 0 :(得分:7)

无法从Google Cloud Messaging服务检索您的应用订阅的主题列表。

您必须跟踪列表并将其保留在您的应用程序(硬编码,存储在首选项,数据库,文件等)或您的服务器上。

当您决定让用户取消订阅时,请从您存储它的位置检索主题列表,并将其传递给{{3>上提到的 unsubscribeWithToken:token:topic:options:handler 页面

答案 1 :(得分:6)

或者,在接收消息时,您可以检查“来自”消息的对象。如果它来自您不再感兴趣的主题,则可以取消订阅而不是处理该消息。

答案 2 :(得分:0)

如果您想取消订阅所有主题,只需执行:

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];

More info

别忘了刷新你的东西!

答案 3 :(得分:0)

如果您有注册令牌,则可以使用https://iid.googleapis.com/iid/info/IID_TOKEN(标题中包含授权码)获取设备订阅的主题。其中IID_TOKEN是注册令牌。

https://developers.google.com/instance-id/reference/server#get_information_about_app_instances找到更多信息。