如何从amazon SNS主题取消订阅iOS设备?
我正在从AmazonWeb服务开发一个带有简单通知服务(SNS)的iOS应用程序。此时,应用程序将设备注册到某个主题,并可以接收推送通知,这些通知将发布到该主题。可以为设备订阅多个主题 现在我正试图从特定主题中取消订阅设备,但SNSUnsubscriberRequest需要一个SubscriptionARN。我尝试从设备中使用EndpointARN,但似乎我必须使用额外的SubscriptionARN来组合EndpointARN和TopicARN。我怎么得到这个阿恩 在本文中:他们要求提供订阅者的完整列表,并将每个EndpointARN与设备的EndpointARN进行比较。我认为这不可能是正确的方式 订阅主题如何从amazon SNS主题取消订阅iOS设备?,ios,amazon-web-services,push-notification,subscription,Ios,Amazon Web Services,Push Notification,Subscription,我正在从AmazonWeb服务开发一个带有简单通知服务(SNS)的iOS应用程序。此时,应用程序将设备注册到某个主题,并可以接收推送通知,这些通知将发布到该主题。可以为设备订阅多个主题 现在我正试图从特定主题中取消订阅设备,但SNSUnsubscriberRequest需要一个SubscriptionARN。我尝试从设备中使用EndpointARN,但似乎我必须使用额外的SubscriptionARN来组合EndpointARN和TopicARN。我怎么得到这个阿恩 在本文中:他们要求提供订阅者
// Check if endpoint exist
if (endpointARN == nil) {
dispatch_async(dispatch_get_main_queue(), ^{
[[self universalAlertsWithTitle:@"endpointARN not found!" andMessage:@"Please create an endpoint for this device before subscribe to topic"] show];
});
return NO;
}
// Create topic if not exist
NSString *topicARN = [self findTopicARNFor:topic];
if (!topicARN) {
[self createTopic:topic];
topicARN = [self findTopicARNFor:topic];
}
// Subscribe to topic if exist
if (topicARN) {
SNSSubscribeRequest *subscribeRequest = [[SNSSubscribeRequest alloc] initWithTopicArn:topicARN andProtocol:@"application" andEndpoint:endpointARN];
SNSSubscribeResponse *subscribeResponse = [snsClient subscribe:subscribeRequest];
if (subscribeResponse.error != nil) {
NSLog(@"Error: %@", subscribeResponse.error);
dispatch_async(dispatch_get_main_queue(), ^{
[[self universalAlertsWithTitle:@"Subscription Error" andMessage:subscribeResponse.error.userInfo.description] show];
});
return NO;
}
}
return YES;
NSString *topicARN = [self findTopicARNFor:topic];
if (topicARN) {
SNSUnsubscribeRequest *unsubscribeRequest = [[SNSUnsubscribeRequest alloc] initWithSubscriptionArn:topicARN];
SNSUnsubscribeResponse *unsubscribeResponse = [snsClient unsubscribe:unsubscribeRequest];
if (unsubscribeResponse.error) {
NSLog(@"Error: %@", unsubscribeResponse.error);
}
}
findTopicARNForTopic方法已经迭代主题列表,并将后缀与主题名称进行比较。我真的不知道这是否是最好的做法
取消订阅主题
// Check if endpoint exist
if (endpointARN == nil) {
dispatch_async(dispatch_get_main_queue(), ^{
[[self universalAlertsWithTitle:@"endpointARN not found!" andMessage:@"Please create an endpoint for this device before subscribe to topic"] show];
});
return NO;
}
// Create topic if not exist
NSString *topicARN = [self findTopicARNFor:topic];
if (!topicARN) {
[self createTopic:topic];
topicARN = [self findTopicARNFor:topic];
}
// Subscribe to topic if exist
if (topicARN) {
SNSSubscribeRequest *subscribeRequest = [[SNSSubscribeRequest alloc] initWithTopicArn:topicARN andProtocol:@"application" andEndpoint:endpointARN];
SNSSubscribeResponse *subscribeResponse = [snsClient subscribe:subscribeRequest];
if (subscribeResponse.error != nil) {
NSLog(@"Error: %@", subscribeResponse.error);
dispatch_async(dispatch_get_main_queue(), ^{
[[self universalAlertsWithTitle:@"Subscription Error" andMessage:subscribeResponse.error.userInfo.description] show];
});
return NO;
}
}
return YES;
NSString *topicARN = [self findTopicARNFor:topic];
if (topicARN) {
SNSUnsubscribeRequest *unsubscribeRequest = [[SNSUnsubscribeRequest alloc] initWithSubscriptionArn:topicARN];
SNSUnsubscribeResponse *unsubscribeResponse = [snsClient unsubscribe:unsubscribeRequest];
if (unsubscribeResponse.error) {
NSLog(@"Error: %@", unsubscribeResponse.error);
}
}
现在,我要求提供整个订户列表,并将EndpointARN与设备的EndpointARN进行比较。我使用以下方法获得订阅arn:
- (NSString *)findSubscriptionARNForTopicARN:(NSString *)topicARN
{
// Iterate over each subscription arn list for a topic arn
NSString *nextToken = nil;
do {
SNSListSubscriptionsByTopicRequest *listSubscriptionRequest = [[SNSListSubscriptionsByTopicRequest alloc] initWithTopicArn:topicARN];
SNSListSubscriptionsByTopicResponse *response = [snsClient listSubscriptionsByTopic:listSubscriptionRequest];
if (response.error) {
NSLog(@"Error: %@", response.error);
return nil;
}
// Compare endpoint arn of subscription arn with endpoint arn of this device
for (SNSSubscription *subscription in response.subscriptions) {
if ([subscription.endpoint isEqualToString:endpointARN]) {
return subscription.subscriptionArn;
}
}
nextToken = response.nextToken;
} while (nextToken != nil);
return nil;
}
使用此方法,我将设备从主题中删除:
- (void)unsubscribeDeviceFromTopic:(NSString *)topic
{
NSString *subscriptionARN = [self findSubscriptionARNForTopic:topic];
if (subscriptionARN) {
SNSUnsubscribeRequest *unsubscribeRequest = [[SNSUnsubscribeRequest alloc] initWithSubscriptionArn:subscriptionARN];
SNSUnsubscribeResponse *unsubscribeResponse = [snsClient unsubscribe:unsubscribeRequest];
if (unsubscribeResponse.error) {
NSLog(@"Error: %@", unsubscribeResponse.error);
}
}
}
您还可以将SubscriptionArn存储在SubscriberResponse中,并在UnSubscribeRequest中使用此值。现在,我要求提供整个订户列表,并将EndpointARN与设备的EndpointARN进行比较。我将在等待8个小时后发布代码…我也遇到了同样的问题,并且在订阅列表中进行了迭代。我担心的是安全问题:我们如何确保只有当前设备被取消订阅?是否有可能其他设备被取消订阅?另外:回答您的解决方案和新代码可能会对其他设备有所帮助。这是不好的。订阅请求返回的arn只是“待定确认”。只有在其他服务确认订阅后,才能创建实际的ARN。@b您可以使用ReturnSubscriptionArn布尔值返回实际的主题ARN。此外,apns/fcm订阅的afaik无需确认。