使用GCM(iOS设备)轻松取消订阅所有订阅主题
我正在尝试通过一个iOS设备中的主题系统推送通知,该设备使用为iOS设备设计的Google云消息传递的新API 我有正确的证书,因此我可以从创建的主题接收通知。我订阅主题的代码如下所示:使用GCM(iOS设备)轻松取消订阅所有订阅主题,ios,google-cloud-messaging,Ios,Google Cloud Messaging,我正在尝试通过一个iOS设备中的主题系统推送通知,该设备使用为iOS设备设计的Google云消息传递的新API 我有正确的证书,因此我可以从创建的主题接收通知。我订阅主题的代码如下所示: if (_registrationToken && _connectedToGCM) { [[GCMPubSub sharedInstance] subscribeWithToken:_registrationToken
if (_registrationToken && _connectedToGCM) {
[[GCMPubSub sharedInstance] subscribeWithToken:_registrationToken
topic:topicToSubscribe
options:nil
handler:^(NSError *error) {
if (error) {
//handle error here
} else {
self.subscribedToTopic = true;
}
}];
}
我知道用于取消订阅的等效函数,但此函数需要一个主题名。
有没有办法检索我的应用可能订阅的所有主题?在订阅之前,请先注销这些主题?没有办法从谷歌云消息服务检索你的应用订阅的主题列表 您必须跟踪列表并将其保存在应用程序(硬编码、存储在首选项、数据库、文件等)或服务器上
当您决定让用户取消订阅时,从存储主题的位置检索主题列表,并将其传递给unsubscribeWithToken:token:topic:options:handler(如页面上所述)或者,在接收消息时,您可以检查消息“发件人”是谁。如果它来自您不再感兴趣的主题,您可以取消订阅,而不是处理消息。如果您想取消订阅所有主题,只需执行:
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];
别忘了刷新您的令牌如果您拥有注册令牌,则可以通过使用(在标题中使用授权密钥)获取设备订阅的主题。其中IID_令牌是注册令牌
查看更多信息。我认为没有一个功能可以检索应用程序已订阅的所有主题。您可以在本地创建一个数组,以表示应用程序订阅的主题列表。因此,在进行
CCMPubSub
订阅或取消订阅呼叫之前,您可以检查数组中是否已经存在主题。谢谢,我做了一些研究,它确认了您的答案。我会照你说的做