问题
First of all I found the same question here, but it doesn't work... Maybe AWS SDK changes or something else, I don't know why... I want to subscribe to SNS topic from my iOS app. I am trying to do it with code from that answer, which I tried to change to get rid of errors:
AWSSNS *sns = [AWSSNS defaultSNS];
AWSSNSCreatePlatformEndpointInput *endpointRequest = [AWSSNSCreatePlatformEndpointInput new];
endpointRequest.platformApplicationArn = @"arn:aws:sns:us-east-1:753780999999:app/APNS_SANDBOX/MyAppDevelopment";
endpointRequest.token = [self deviceTokenAsString:deviceToken];
[[[sns createPlatformApplication:endpointRequest] continueWithSuccessBlock:^id(AWSTask *task) {
AWSSNSCreateEndpointResponse *response = task.result;
AWSSNSSubscribeInput *subscribeRequest = [AWSSNSSubscribeInput new];
subscribeRequest.endpoint = response.endpointArn;
subscribeRequest.protocols = @"application";
subscribeRequest.topicArn = @"arn:aws:sns:us-east-1:753780999999:MyAppDevelopingTest";
return [sns subscribe:subscribeRequest];
}] continueWithBlock:^id(AWSTask *task) {
if (task.cancelled) {
NSLog(@"Task cancelled");
}
else if (task.error) {
NSLog(@"Error occurred: [%@]", task.error);
}
else {
NSLog(@"Success");
}
return nil;
}];
But I get the error:
Error occurred: [Error Domain=com.amazonaws.AWSSNSErrorDomain Code=0 "The operation couldn’t be completed. (com.amazonaws.AWSSNSErrorDomain error 0.)" UserInfo=0x17ee0950 {Type=Sender, Message=3 validation errors detected: Value null at 'name' failed to satisfy constraint: Member must not be null; Value null at 'attributes' failed to satisfy constraint: Member must not be null; Value null at 'platform' failed to satisfy constraint: Member must not be null, __text=(
"\n ",
"\n ",
"\n ",
"\n "
), Code=ValidationError}]
Why so? Why does the resource cut after app/ is it ok? Beside this I don't understand where I should put deviceToken? I really need help! Thanks in advance?
My Cognito role is:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"sns:CreatePlatformEndpoint",
"sns:Subscribe",
"sns:CreatePlatformApplication",
"sns:*"
],
"Resource": [
"arn:aws:sns:*"
]
}
]
}
I am using the latest AWS SDK:
Installing AWSCognito 2.2.1 (was 2.2.0)
Installing AWSCore 2.2.1 (was 2.2.0)
Installing AWSSNS 2.2.1 (was 2.2.0)
回答1:
Sorry, unfortunately you copied code that included a typo:
createPlatformApplication:endpointRequest
Should be:
createPlatformEndpoint:endpointRequest
The method CreatePlatformApplication was not allowed in your original policy. Once you allowed sns:*, the call was allowed by the service, but the request didn't include the required parameters for CreatePlatformApplication, hence the ValidationError. The trimmed ARN in your original message is also a result of lack of parameters for CreatePlatformApplication.
来源:https://stackoverflow.com/questions/31300136/amazon-aws-sns-how-do-i-subscribe-to-sns-topic-from-ios