vendredi 7 août 2015

AWS SNS createPlatformEndpoint returns nil endpointArn after upgrading to AWS iOS SDK to 2.2.3

I recently upgraded the AWS iOS SDK to 2.2.3 in a working application that was using Amazon SNS with AWS SDK 2.1.1. I got a lot of compile errors regarding to BFTask. I figured out that I should change all the BFTask names in my code to AWSTask. Then everything compiled and I could run the application. But now I'm getting a problem at runtime. When I'm creating an SNS endpoint, the returned AWSTask doesn't show error, nor exception. It's successful with a non-nil result. But the endpointARN in the result is nil! I have created my SNS app from the console.

Here's the relevant fragment of the policy that I'm using:

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

And, here's my code in 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
    }
}

I would appreciate your help. Thanks!




Aucun commentaire:

Enregistrer un commentaire