1
votes

After a device registers for remote notifications (in application:didRegisterForRemoteNotificationsWithDeviceToken) I am:

  1. Creating a platform endpoint using the device's token.
  2. Subscribing to a topic.
  3. Attempting* to confirm topic subscription

I am stuck on step 3 from the above list. I can't figure out how to get the token needed to create a "AWSSNSConfirmSubscriptionInput" object to pass to "confirmSubscription".

I should note that I am able to send APNs from the topic despite the fact that the subscription is not confirmed. Is this not a necessary step in the context of iOS (application protocol)?

The documentation states following:

To actually create a subscription, the endpoint owner must call the ConfirmSubscription action with the token from the confirmation message. Confirmation tokens are valid for three days.

I can't figure out how to obtain the "token" necessary to perform the "ConfirmSubscription" action. The "Subscribe" action result is of type "AWSSNSSubscribeResponse" which only has a single propery, " subscriptionArn" and does not appear to contain a "confirmation message" including a token. So how do I obtain this token?

Here is the body of my application:didRegisterForRemoteNotificationsWithDeviceToken delegate method:

sns.createPlatformEndpoint(createPlatformEndPointInput).continueWithBlock { (task:AWSTask!) in
        if let error = task.error{
            XCGLogger.defaultInstance().error("Platform endpoint creation error: \(error)")
            return nil
        }
        let result = task.result as! AWSSNSCreateEndpointResponse
        let subscribeInput = AWSSNSSubscribeInput()
        subscribeInput.topicArn = "arn:aws:sns:us-west-2:xxx:topicname"
        subscribeInput.endpoint = result.endpointArn
        subscribeInput.protocols = "application"
        sns.subscribe(subscribeInput).continueWithBlock({ (task:AWSTask!) in
            if let subError = task.error{
                XCGLogger.defaultInstance().error("Topic Subscription Error: \(subError)")
                return nil
            }
            let subscribeResult = task.result as! AWSSNSSubscribeResponse
            XCGLogger.defaultInstance().debug("Subscription result: \(subscribeResult)")
            let subscriptionConfirmInput = AWSSNSConfirmSubscriptionInput()
            subscriptionConfirmInput.token = ?? //How do I get this??
            subscriptionConfirmInput.topicArn = "arn:aws:sns:xxx:myTopic"
            sns.confirmSubscription(subscriptionConfirmInput).continueWithBlock({ (task:AWSTask!) in
                if let error = task.error{
                    XCGLogger.defaultInstance().error("Subscription Confirmation Error: \(error)")
                }
                return nil
            })
            return nil
        })
        return nil
    }
1

1 Answers

1
votes

Subscribe returns SubscriptionArn if the service was able to create a subscription immediately (without requiring endpoint owner confirmation). For mobile devices, this should be the case.

As a side note, when registering the device, you should follow the pseudo code in this blog post. AWS Mobile Hub can produce a reference implementation for registering the device and subscribing to a topic using the recommended workflow as well.