Skip to content Skip to sidebar Skip to footer

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 wou

Solution 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:

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:

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

privateclassRegisterIdForAWSextendsAsyncTask<String, Void, Void> {

        private Exception exception;

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

                if (pushNotificationRegId != null) {

                    CognitoCachingCredentialsProviderprovider=newCognitoCachingCredentialsProvider(
                            getApplicationContext(),
                            "us-west-2:aaaaaaaaa-1234-1234-1234-0bbbbbbbbbbbb",
                            Regions.US_WEST_2);
                    StringplatformApplicationArn="arn:aws:sns:us-west-2:123456789:app/GCM/appname";
                    AmazonSNSClientpushClient=newAmazonSNSClient(provider);
                    pushClient.setRegion(Region.getRegion(Regions.US_WEST_2));

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

            returnnull;
        }

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

Post a Comment for "Register Device Id Directly With Amazon Sns"