How to unsubscribe an iOS Device from an amazon SNS topic?

依然范特西╮ 提交于 2019-12-04 03:32:01

For now I ask for the whole subscriber list and compare the EndpointARN with the EndpointARN of the Device. With the following method i get the subscription 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;
}

and with this method i remove the device from a topic:

- (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);
        }
    }
}

You could also store the SubscriptionArn in the SubscribeResponse and use this value in the UnSubscribeRequest.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!