'Register device id directly with Amazon SNS

I am using the Amazon Web Service to send push notifications directly to a device. After I install the app I get the device id, that I need to manually add to the Amazon SNS. I would like to know if there is anyway to register the device id directly with the amazon server the moment the user starts the application.

I have read this, but found it difficult to understand. Does anyone have any previous experience of how to do this?

EDIT 2 (What I have done so far)

I've followed the instructions from this link

  1. I download the snspobilepush.zip file as instructed and extract and import the project into eclipse. I add the GCM project number, add the jar files and run the application. I get my device registration Id.

  2. I open the Amazon SNS, add my device id and I publish a message. I receive the message on my mobile phone. Works great so far.

    MY PROBLEM

I would be having a lot of potential users for my application. So adding every device id manually to the SNS makes no sense. I need the Amazon SNS to directly register my device id when I start the app. Is there any option for me to do that? I couldn't find any definitive answer in the docs.
This link tells me to Use the "AWS Token Vending Service". However, I could not find any example of how to do that.



Solution 1:[1]

Using the AmazonSNSClient documented here:

http://docs.aws.amazon.com/AWSAndroidSDK/latest/javadoc/

it should be possible to register using code similar to this:

AWSCredentials awsCredentials = new BasicAWSCredentials("XXXXXX", "XXXXXXXXXXXXXXX");
String platformApplicationArn = "arn:aws:sns:us-east-1:123456789:app/GCM/myappname";
AmazonSNSClient pushClient = new AmazonSNSClient(awsCredentials);

String customPushData = "my custom data";
CreatePlatformEndpointRequest platformEndpointRequest = new CreatePlatformEndpointRequest();
platformEndpointRequest.setCustomUserData(customPushData);
platformEndpointRequest.setToken(pushNotificationRegId);
platformEndpointRequest.setPlatformApplicationArn(platformApplicationArn);
CreatePlatformEndpointResult result = pushClient.createPlatformEndpoint(platformEndpointRequest);
Log.w(TAG, "Amazon Push reg result: " + result);

It was not liking my ARN, but that was a stupid typo that Reid pointed out and is now fixed above.

Solution 2:[2]

There is Android AWS SDK available to use. Check out the documentation link: http://docs.aws.amazon.com/AWSAndroidSDK/latest/javadoc/

Also, more information available: http://aws.amazon.com/sdkforandroid/

Solution 3:[3]

This is working for Firebase and Cognito. An AsyncTask is necessary to avoid running on the Main Thread.

private class RegisterIdForAWS extends AsyncTask<String, Void, Void> {

        private Exception exception;

        protected Void doInBackground(String... urls) {
            try {
                String pushNotificationRegId = FirebaseInstanceId.getInstance().getToken();

                if (pushNotificationRegId != null) {

                    CognitoCachingCredentialsProvider provider = new CognitoCachingCredentialsProvider(
                            getApplicationContext(),
                            "us-west-2:aaaaaaaaa-1234-1234-1234-0bbbbbbbbbbbb",
                            Regions.US_WEST_2);
                    String platformApplicationArn = "arn:aws:sns:us-west-2:123456789:app/GCM/appname";
                    AmazonSNSClient pushClient = new AmazonSNSClient(provider);
                    pushClient.setRegion(Region.getRegion(Regions.US_WEST_2));

                    String customPushData = "";
                    CreatePlatformEndpointRequest platformEndpointRequest = new CreatePlatformEndpointRequest();
                    platformEndpointRequest.setCustomUserData(customPushData);
                    platformEndpointRequest.setToken(pushNotificationRegId);
                    platformEndpointRequest.setPlatformApplicationArn(platformApplicationArn);
                    CreatePlatformEndpointResult result = pushClient.createPlatformEndpoint(platformEndpointRequest);
                    Log.w(TAG, "Amazon Push reg result: " + result);
                }
            } catch (Exception e) {
                this.exception = e;
            }

            return null;
        }

        protected void onPostExecute(String text) {
            Log.w(TAG, "Amazon Push reg Finished");
        }
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hjpotter92
Solution 2 TeaCupApp
Solution 3 thejuki