Ios AMAZON AWS如何订阅SNS主题的端点?

Ios AMAZON AWS如何订阅SNS主题的端点?,ios,amazon-web-services,push-notification,amazon-sns,amazon-cognito,Ios,Amazon Web Services,Push Notification,Amazon Sns,Amazon Cognito,我正在使用Amazon SNS和Amazon Cognito服务在iOS应用程序中实现推送通知。 Cognito成功保存代币,我的应用程序收到通知,一切正常,但有一件事 现在,在开发过程中,我需要手动将端点添加到SNS主题,以便所有订阅者都可以获得通知。当我将更新推送到应用商店时,将有数千个令牌需要添加 我当时正在研究AmazonAWS文档,但不知道是否有可能在没有额外努力的情况下实现它 我的问题是:是否可以通过Amazon services自动订阅主题的端点?无法自动订阅主题的端点,但您可以通

我正在使用Amazon SNS和Amazon Cognito服务在iOS应用程序中实现推送通知。 Cognito成功保存代币,我的应用程序收到通知,一切正常,但有一件事

现在,在开发过程中,我需要手动将端点添加到SNS主题,以便所有订阅者都可以获得通知。当我将更新推送到应用商店时,将有数千个令牌需要添加

我当时正在研究AmazonAWS文档,但不知道是否有可能在没有额外努力的情况下实现它


我的问题是:是否可以通过Amazon services自动订阅主题的端点?

无法自动订阅主题的端点,但您可以通过代码完成所有操作

创建端点后,可以直接调用
Subscribe
API。与其他类型的订阅不同,SNS移动推送不需要确认

下面是一些示例Objective-C代码,用于创建端点并将其订阅到主题:

AWSSNS *sns = [AWSSNS defaultSNS];
AWSSNSCreatePlatformEndpointInput *endpointRequest = [AWSSNSCreatePlatformEndpointInput new];
endpointRequest.platformApplicationArn = MY_PLATFORM_ARN;
endpointRequest.token = MY_TOKEN;

[[[sns createPlatformEndpoint:endpointRequest] continueWithSuccessBlock:^id(AWSTask *task) {
    AWSSNSCreateEndpointResponse *response = task.result;

    AWSSNSSubscribeInput *subscribeRequest = [AWSSNSSubscribeInput new];
    subscribeRequest.endpoint = response.endpointArn;
    subscribeRequest.protocols = @"application";
    subscribeRequest.topicArn = MY_TOPIC_ARN;
    return [sns subscribe:subscribeRequest];
}] continueWithBlock:^id(BFTask *task) {
    if (task.cancelled) {
        NSLog(@"Task cancelled");
    }
    else if (task.error) {
        NSLog(@"Error occurred: [%@]", task.error);
    }
    else {
        NSLog(@"Success");
    }
    return nil;
}];
确保您已在Cognito角色中授予对
sns:Subscribe
的访问权,以允许您的应用程序进行此调用


更新2015-07-08:更新以反映AWS iOS SDK 2.2.0+

如果您想使用静态凭据而不是AWSCognito,则需要通过Amazon IAM控制台创建这些凭据

以下是在应用程序代理中初始化Amazon的代码

    // Sets up the AWS Mobile SDK for iOS
    // Initialize the Amazon credentials provider
    AWSStaticCredentialsProvider *credentialsProvider =[[AWSStaticCredentialsProvider alloc] initWithAccessKey:AWSAccessID secretKey:AWSSecretKey];

    AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:DefaultServiceRegionType credentialsProvider:credentialsProvider];

    [AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;

Fissh

这是在
Swift3

 func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

    //Get Token ENDPOINT
    let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})

    //Create SNS Module
    let sns = AWSSNS.default()
    let request = AWSSNSCreatePlatformEndpointInput()
    request?.token = deviceTokenString

    //Send Request
    request?.platformApplicationArn = Constants.SNSDEVApplicationARN

    sns.createPlatformEndpoint(request!).continue({ (task: AWSTask!) -> AnyObject! in
        if task.error != nil {
            print("Error: \(task.error)")
        } else {

            let createEndpointResponse = task.result! as AWSSNSCreateEndpointResponse
            print("endpointArn: \(createEndpointResponse.endpointArn)")

            let subscription = Constants.SNSEndPoint //Use your own topic endpoint

            //Create Subscription request
            let subscriptionRequest = AWSSNSSubscribeInput()


              subscriptionRequest?.protocols = "application"
                subscriptionRequest?.topicArn = subscription
                subscriptionRequest?.endpoint = createEndpointResponse.endpointArn

                sns.subscribe(subscriptionRequest!).continue ({
                    (task:AWSTask) -> AnyObject! in
                    if task.error != nil
                    {
                        print("Error subscribing: \(task.error)")
                        return nil
                    }

                    print("Subscribed succesfully")

                   //Confirm subscription
                    let subscriptionConfirmInput = AWSSNSConfirmSubscriptionInput()
                    subscriptionConfirmInput?.token = createEndpointResponse.endpointArn
                    subscriptionConfirmInput?.topicArn = subscription
                    sns.confirmSubscription(subscriptionConfirmInput!).continue ({
                        (task:AWSTask) -> AnyObject! in
                        if task.error != nil
                        {
                            print("Error subscribing: \(task.error)")
                        }
                        return nil
                    })
                    return nil

                })

            }
            return nil

        })
    }

你能澄清一下吗?你所说的仅使用亚马逊服务是什么意思?你是在尝试自动订阅一个主题,并使用Cognito作为凭证来实现这一点吗?@JeffBailey谢谢你的提问。我已经弄明白了,但没有足够的时间自我回答。我想自动订阅,但我不知道我的iOS应用程序负责订阅一个主题的端点,我想在调用cognito保存令牌后,可以通过Amazon AWS控制台来保存令牌。“但是没有足够的时间自行回答。”所以你不打算分享你想出的答案?那太可怕了,在别人帮忙之后。做一个好公民,分享你的解决方案。我自己已经解决了,这是正确的(也是唯一的?)方法。无论如何,谢谢你,你的回答将来可能会对其他人有所帮助。@cyborg86pl可以通过控制台订阅,但这更多的是一个手动过程。Bob,你能告诉我为什么我的订阅数量停止在2000,而应用程序端点仍在增长吗?我知道主题有10万个限制,那么问题出在哪里?@cyborg86pl如果您在控制台中查看,控制台仅显示2000个订阅的限制。如果您需要查看所有订阅,则需要使用原始API。好的,谢谢,因此无需担心。如果Amazon给出了一个提示,就像我现在在“订阅”选项卡上看到的那样,有10100个限制,那就太好了。不确定这是否是个好主意,AWS SDK表示在生产中不要使用AWSStaticCredentialsProvider:
我们强烈反对在生产应用程序中嵌入AWS凭据,因为它们很容易被提取和滥用。考虑使用“AWSCONITIOC证书提供者”< /代码>我完全同意Popei,我应该提到警告。这是应用AWS服务时的基本考虑,当然不是以上问题的答案。