【问题标题】:How to unsubscribe an iOS Device from an amazon SNS topic?如何从亚马逊 SNS 主题退订 iOS 设备?
【发布时间】:2014-04-07 10:27:26
【问题描述】:

我正在使用 Amazon Web Services 的简单通知服务 (SNS) 开发 iOS 应用程序。此时,应用程序将设备注册到 Topic 并可以接收推送通知,这些推送通知会发布到 Topic。可以为设备订阅多个主题。

现在我正在尝试从特定主题退订设备,但 SNSUnsubscribeRequest 需要 SubscriptionARN。我尝试使用设备中的 EndpointARN,但似乎我必须使用额外的 SubscriptionARN 来组合 EndpointARN 和 TopicARN。如何获得此 ARN?

在这篇文章中:How do you get the arn of a subscription? 他们要求提供完整的订阅者列表,并将每个 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 进行比较。等8小时后我会发布代码...
  • 我遇到了同样的问题,并且通过订阅者列表进行迭代修复了它。我关心的是安全性:我们如何确定只有当前设备被取消订阅?是否有可能其他设备被取消订阅?
  • 另外:您的解决方案的答案和新代码可能对其他人有帮助。

标签: ios amazon-web-services push-notification subscription


【解决方案1】:

现在我要求提供整个订阅者列表,并将 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);
        }
    }
}

【讨论】:

    【解决方案2】:

    您还可以将 SubscriptionArn 存储在 SubscribeResponse 中,并在 UnSubscribeRequest 中使用此值。

    【讨论】:

    • 这不好。订阅请求返回的 arn 只是“等待确认”。在其他服务确认订阅之前,不会创建实际的 ARN。
    • @BrianC 您可以使用 ReturnSubscriptionArn 布尔值来返回实际的主题 arn。 apns/fcm 订阅的 afaik 也不需要确认。
    猜你喜欢
    • 1970-01-01
    • 2015-02-21
    • 2020-05-19
    • 2017-03-08
    • 1970-01-01
    • 2015-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多