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

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

概述我正在使用Amazon Web Services的简单通知服务(SNS)开发iOS应用程序.此时,应用程序将设备注册到主题,并可以接收推送通知,这些通知将发布到主题.可以将设备订阅到许多主题. 现在我正在尝试从特定主题取消订阅设备,但SNSUnsubscribeRequest需要SubscriptionARN.我试图从设备中使用EndpointARN,但似乎我要为EndpointARN和Topic 我正在使用Amazon Web Services的简单通知服务(SNS)开发iOS应用程序.此时,应用程序将设备注册到主题,并可以接收推送通知,这些通知将发布到主题.可以将设备订阅到许多主题.

现在我正在尝试从特定主题取消订阅设备,但SNSUnsubscribeRequest需要SubscriptionARN.我试图从设备中使用EndpointARN,但似乎我要为EndpointARN和topicARN的组合使用额外的SubscriptionARN.我如何获得此ARN?

在这篇文章中:How do you get the arn of a subscription?他们要求提供整个订阅者列表,并将每个EndpointARN与设备的EndpointARN进行比较.这不可能是我想的正确方法.

订阅主题

// Check if endpoint existif (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 existNsstring *topicARN = [self findtopicARNFor:topic];if (!topicARN) {    [self createtopic:topic];    topicARN = [self findtopicARNFor:topic];}// Subscribe to topic if existif (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);        }    }}
总结

以上是内存溢出为你收集整理的如何从亚马逊SNS主题取消订阅iOS设备?全部内容,希望文章能够帮你解决如何从亚马逊SNS主题取消订阅iOS设备?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1103943.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存