简体   繁体   English

Amazon AWS SNS:如何从iOS订阅SNS主题?

[英]Amazon AWS SNS: How do i subscribe to SNS topic from iOS?

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. 首先,我在这里找到了相同的问题,但它不起作用...也许AWS SDK发生了更改或其他原因,我不知道为什么...我想从我的iOS应用程序订阅SNS主题。 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? 为什么在app /之后可以减少资源? Beside this I don't understand where I should put deviceToken? 除此之外,我不知道应该将deviceToken放在哪里? I really need help! 我真的需要帮助! Thanks in advance? 提前致谢?

My Cognito role is: 我的Cognito角色是:

{
    "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: 我正在使用最新的AWS开发工具包:

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)

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. 您的原始策略中不允许使用CreatePlatformApplication方法。 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. 一旦允许sns:*,该服务就允许该调用,但是请求不包含CreatePlatformApplication的必需参数,因此不包括ValidationError。 The trimmed ARN in your original message is also a result of lack of parameters for CreatePlatformApplication. 原始消息中经过修剪的ARN也是由于缺少CreatePlatformApplication参数的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM