1

I'm trying to store my device token from my android app programmatically to AWS SNS platform application.

I'm getting an error in getApplicationContext() method. Anyone with a solution for this error?

This is my code:

public class RegisterIdForAWS extends AsyncTask<String, Void, Void> {
    private Exception exception;

    @Override
    protected Void doInBackground(String... strings) {
        try {
            String pushNotificationRegId = FirebaseInstanceId.getInstance().getToken();

            if (pushNotificationRegId != null) {

                CognitoCachingCredentialsProvider provider = new CognitoCachingCredentialsProvider(
                       getApplicationContext(),
                        "us-west-2:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
                        Regions.US_WEST_2);
                String platformApplicationArn = "arn:aws:sns:us-west-2:11111111111:app/GCM/123";
                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");
    }


}
Shreeya Chhatrala
  • 1,441
  • 18
  • 33
Ashish
  • 167
  • 1
  • 1
  • 6
  • In which Activity you are using this AsyncTask and give proper flow with details so answer can be deliverd properly. Otherwise answer below given by the @Andrea Ebano was correct. – Vishal Gedia Jul 27 '17 at 09:15

1 Answers1

0

In constructor of your async class, pass Context

private Context mContext;

public RegisterIdForAWS (Context context){
     mContext = context;
}

And instantiate class like this:

RegisterIdForAWS task = new RegisterIdForAWS (context);

Hope it helps.

Andrea Ebano
  • 563
  • 1
  • 4
  • 16