我正在使用Amazon SNS和Amazon Cognito服务在iOS应用中实现推送通知。 Cognito成功保存令牌,我的应用程序得到通知,一切正常,但有一件事。
现在,当我还在开发中时,我需要手动将端点添加到SNS主题,因此所有订阅者都可以收到通知。当我将更新推送到App Store时,将会有数千个令牌要添加。
我正在研究亚马逊AWS文档,但不知道是否可以在没有额外工作的情况下实现它。
我的问题:是否可以自动订阅端点到主题为仅的?
答案 0 :(得分:23)
无法自动订阅某个主题的端点,但您可以完成所有代码。
您可以在创建端点后直接调用Subscribe
API。与其他类型的订阅不同,SNS Mobile Push无需确认。
以下是一些示例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 +
答案 1 :(得分: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
})
}
答案 2 :(得分:1)
如果您想使用静态凭证而不是使用AWSCognito,则需要通过Amazons 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;
Fissh