如何从亚马逊SNS主题取消订阅iOS设备?

我正在使用Amazon Web Services的Simple Notification Service(SNS)开发iOS应用程序。 此时,应用程序将设备注册到主题,并可以接收发布到主题的推送通知。 可以将设备订阅到许多主题。

现在我试图从特定主题取消订阅设备,但SNSUnsubscribeRequest需要一个SubscriptionARN。 我尝试使用设备上的EndpointARN,但似乎我必须为EndpointARN和TopicARN的组合使用额外的SubscriptionARN。 我如何获得这个ARN?

在这篇文章中:你如何获得订阅的阿恩? 他们要求整个订户列表,并将每个EndpointARN与设备的EndpointARN进行比较。 这不能是我认为正确的方式。

订阅主题

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

findTopicARNForTopic方法已遍历主题列表并将后缀与主题名称进行比较。 我真的不知道这是否是最佳做法。

取消订阅主题

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存储在SubscribeResponse中,并在UnSubscribeRequest中使用此值。

链接地址: http://www.djcxy.com/p/32207.html

上一篇: How to unsubscribe an iOS Device from an amazon SNS topic?

下一篇: Amazon SNS signature verification not working