升级到AWS iOS SDK到2.2.3后,AWS SNS createPlatformEndpoint返回nil endpointArn

时间:2015-08-07 22:04:09

标签: ios amazon-web-services amazon-sns

我最近在使用Amazon SNS和AWS SDK 2.1.1的工作应用程序中将AWS iOS SDK升级到2.2.3。我收到了很多关于BFTask的编译错误。我发现我应该将代码中的所有BFTask名称更改为AWSTask。然后编译了一切,我可以运行应用程序。但是现在我在运行时遇到了问题。当我创建SNS端点时,返回的AWSTask不会显示错误,也不会显示异常。这是成功的非零结果。但结果中的endpointARN为零!我从控制台创建了我的SNS应用程序。

以下是我正在使用的政策的相关片段:

    {
        "Effect": "Allow",
        "Action": [
            "SNS:CreatePlatformEndpoint"
        ],
        "Resource": [
            "My application ARN"
        ]
    },
    {
        "Effect": "Allow",
        "Action": [
            "SNS:Subscribe"
        ],
        "Resource": [
            "My topic"
        ]
    } 

而且,这是我在Swift中的代码:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Some code...
    // ...

    UIApplication.sharedApplication().registerForRemoteNotifications()

    // Some code...
}

    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {

    // remove the spaces from the device token
    var token = deviceTokenAsString(deviceToken)

    var platformEndpointRequest = AWSSNSCreatePlatformEndpointInput()
    platformEndpointRequest.token = token as String
    platformEndpointRequest.platformApplicationArn = SNS_APP_ARN

    let snsManager = AWSSNS.defaultSNS()
    snsManager.createPlatformEndpoint(platformEndpointRequest).continueWithBlock { (task: AWSTask!) -> AnyObject! in
        if task.error != nil {

            NSLog("createPlatformEndpoint failed: \(task.error)")
        } else if task.exception != nil {

            NSLog("createPlatformEndpoint failed: \(task.exception)")
        } else if task.result != nil {

            NSLog("task.result: \(task.result)")
            let createEndpointResponse = task.result as! AWSSNSCreateEndpointResponse
            NSLog("createEndpointResponse: \(createEndpointResponse)")
            var endpointARN = createEndpointResponse.endpointArn
            NSLog("createPlatformEndpoint succeeded, endpoint ARN: \(endpointARN)")

            // Some more code...
        }
        return nil

        }.continueWithBlock { (task: AWSTask!) -> AnyObject! in

            if task.error != nil{

                NSLog("sunscribe failed: \(task.error)")
            } else if task.exception != nil {

                NSLog("sunscribe failed: \(task.exception)")
            } else if task.result != nil {

                NSLog("sunscribe succeeded")
            }

            return nil
    }
}

感谢您的帮助。谢谢!

1 个答案:

答案 0 :(得分:1)

这里我用swift 3更新了版本sdk 2.4.1 订阅端点到主题

    let credentialsProvider = AWSCognitoCredentialsProvider(regionType: .usEast1, identityPoolId: SNSItentityBoolId)
        let configuration = AWSServiceConfiguration(region: .usEast1, credentialsProvider: credentialsProvider)
        AWSServiceManager.default().defaultServiceConfiguration = configuration
        let sns = AWSSNS.default()
        let request = AWSSNSCreatePlatformEndpointInput()
        request?.token = self.strDeviceToken

        //Send Request
        request?.platformApplicationArn = SNSPlatformApplicationArn

        sns.createPlatformEndpoint(request!).continue({ (task: AWSTask!) -> AnyObject! in
            print("blah")
            if task.error != nil {
                print("Error: \(task.error)")
            } else {

                let createEndpointResponse = task.result! as AWSSNSCreateEndpointResponse
                print("endpointArn: \(createEndpointResponse.endpointArn)")

                let subscription = "*****" //Use your own topic endpoint

                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")
                    print("Success Message \(task.result)")
//                    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

        })