亚马逊AWS SNS:我如何从iOS订阅SNS主题?

时间:2015-07-08 17:54:52

标签: ios objective-c amazon-web-services amazon-sns amazon-cognito

首先,我发现了相同的问题here,但它不起作用......也许是AWS SDK更改或其他内容,我不知道为什么...我想要从我的iOS应用程序订阅SNS主题。我试图用这个答案的代码来做,我试图改变以消除错误:

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;

    }];

但我收到错误:

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

为什么这样?为什么app /之后资源减少了?除此之外,我不明白我应该把deviceToken放在哪里? 我真的需要帮助!提前谢谢?

我的Cognito角色是:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "sns:CreatePlatformEndpoint",
                "sns:Subscribe",
                "sns:CreatePlatformApplication",
                "sns:*"
            ],
            "Resource": [
                "arn:aws:sns:*"
            ]
        }
    ]
}

我使用的是最新的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 个答案:

答案 0 :(得分:1)

很抱歉,很遗憾您复制了包含拼写错误的代码:

createPlatformApplication:endpointRequest

应该是:

createPlatformEndpoint:endpointRequest

原始策略中不允许使用CreatePlatformApplication方法。一旦你允许sns:*,服务允许调用,但请求没有包含CreatePlatformApplication所需的参数,因此ValidationError。原始消息中的裁剪ARN也是缺少CreatePlatformApplication参数的结果。