user2732722
user2732722

Reputation: 655

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!

Upvotes: 0

Views: 1259

Answers (1)

karthikeyan
karthikeyan

Reputation: 3898

Here i have updated version sdk 2.4.1 with swift 3 subscribe an endpoint to a topic

    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

        })

Upvotes: 1

Related Questions