14

我正在使用 Amazon SNS 和 Amazon Cognito 服务的 iOS 应用程序中实施推送通知。Cognito 成功保存了令牌,我的应用程序收到通知,一切正常,但有一点。

现在,仍处于开发阶段时,我需要手动将端点添加到 SNS 主题,以便所有订阅者都能收到通知。当我将更新推送到 App Store 时,将有数千个令牌要添加。

我正在研究 Amazon AWS 文档,但不知道是否有可能在不付出额外努力的情况下实现它。

我的问题:是否可以仅使用 Amazon 服务自动为端点订阅主题?

4

3 回答 3

23

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

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+

于 2014-11-18T17:49:38.910 回答
3

这是将端点订阅到主题的原始代码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

        })
    }
于 2016-11-11T15:17:44.273 回答
1

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

这是在您的 App Delegate 中初始化 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;

于 2015-04-23T03:02:24.393 回答