Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 views
in Technique[技术] by (71.8m points)

ios - AMAZON AWS How do i subscribe an endpoint to SNS topic?

I'm implementing push notifications in an iOS app using Amazon SNS and Amazon Cognito services. Cognito saves tokens successfully, my app gets notified, everything's working well, but there is a thing.

Now, when still in development, I need to manually add endpoints to an SNS topic, so all subscribers can get notifications. When i'll push an update to the App Store, there will be thousands of tokens to add.

I was studying Amazon AWS documentation, but there was no clue whether it's possible to make it happen without that additional effort.

My question: is it possible to automatically subscribe an endpoint to a topic with Amazon services only?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is no way to automatically subscribe an endpoint to a topic, but you can accomplish all through code.

You can directly call the Subscribe API after you have created your endpoint. Unlike other kinds of subscription, no confirmation is necessary with SNS Mobile Push.

Here is some example Objective-C code that creates an endpoint and subscribes it to a topic:

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

Make sure you have granted access to sns:Subscribe in your Cognito roles to allow your application to make this call.

Update 2015-07-08: Updated to reflect AWS iOS SDK 2.2.0+


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...