AMAZON AWS我如何订阅端点到SNS主题?

我正在使用Amazon SNS和Amazon Cognito服务在iOS应用程序中实施推送通知。 Cognito成功保存令牌,我的应用程序得到通知,一切运作良好,但有一件事情。

现在,在开发过程中,我需要手动将端点添加到SNS主题,以便所有订阅者都可以收到通知。 当我将更新推向App Store时,将会添加数千个令牌。

我正在研究亚马逊AWS文档,但是没有线索可以在没有额外努力的情况下实现。

我的问题:是否有可能自动订阅端点到一个只有亚马逊服务的主题?

没有办法自动订阅端点到一个主题,但你可以通过代码完成所有。

您可以在创build完端点后直接调用Subscribe API。 与其他types的订阅不同,SNS Mobile Push不需要确认。

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

 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; }]; 

确保您已授予访问sns:Subscribe权限sns:Subscribe在您的Cognitoangular色中sns:Subscribe ,以允许您的应用程序进行此调用。

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

这是在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 }) } 

如果你想使用静态证书,而不是使用AWSCognito,你将需要通过亚马逊IAM控制台创build。

以下是在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; 

Fissh