1

I have followed a tutorial on how to setup SNS Push notification but the CreatePlatformEndpointResult object returns null. I need that so that I can retrieve the endpointArn and send that to the backend. Below is my entire setup. And here is the link to the tutorial: http://www.allcode.com/amazon-sns-push-notification-tutorial-android-using-gcm/

First I retrieve a token from GCM by calling

AWSManager.registerAppToGCM(getApplicationContext())

This is from my AWSManager class

public class AWSManager {
    private static final String TAG = AWSManager.class.getSimpleName();
    private static final String SNS_ACCESS_KEY_ID = "1234567890"; // I have swapped out the real key
    private static final String SNS_SECRET_KEY = "1234567890"; // I have swapped out the real key

    private static AmazonSNSClient snsClient;

    /**
     * Method is used to retrieve SNSClient object
     *
     * @return snsClient object
     */
    public static AmazonSNSClient getSNSClient() {
        if (FrameworkUtils.checkIfNull(snsClient)) {
            snsClient = new AmazonSNSClient(new BasicAWSCredentials(SNS_ACCESS_KEY_ID, SNS_SECRET_KEY));
            snsClient.setRegion(Region.getRegion(Regions.US_WEST_1));
        }
        return snsClient;
    }

    /**
     * Method is used to register app to GCM
     *
     * @param context
     */
    public static void registerAppToGCM(Context context) {
        SharedPref sharedPref = new SharedPref(context, Constants.PREF_FILE_NAME);
        String gcmToken = sharedPref.getStringPref(Constants.NOTIFICATION_GCM_TOKEN, "");
        if (FrameworkUtils.isStringEmpty(gcmToken)) {
            new GCMRegisterTask(context).execute();
        }
    }
}

Here is the class performing the background task

public class GCMRegisterTask extends AsyncTask<String, Void, Boolean> {
    private static final String TAG = GCMRegisterTask.class.getSimpleName();

    private Context mContext;

    /**
     * Constructor
     *
     * @param context
     */
    public GCMRegisterTask(Context context) {
        super();
        mContext = context;
    }

    @Override
    protected Boolean doInBackground(String... params) {
        String token;
        try {
            token = InstanceID.getInstance(mContext).getToken(mContext.getString(R.string.gcm_project_id), GoogleCloudMessaging.INSTANCE_ID_SCOPE);
            SharedPref sharedPref = new SharedPref(mContext, Constants.PREF_FILE_NAME);
            sharedPref.setPref(Constants.NOTIFICATION_GCM_TOKEN, token);
            Logger.i(TAG, "GCM token successfully stored to prefs: " + token);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}

Once I have successfully retrieved the GCM token. I use new

AWSCreateEndpointTask(mContext).execute(test, token, "email@gmail.com")

to begin the process of creating the endpoint ARN. test = "arn:aws:sns:region:us-east-1:app/GCM/AppName"

public class AWSCreateEndpointTask extends AsyncTask<String, Void, CreatePlatformEndpointResult> {

    Context mContext;

    /**
     * Constructor
     *
     * @param context
     */
    public AWSCreateEndpointTask(Context context) {
        super();
        mContext = context;
    }

    @Override
    protected CreatePlatformEndpointResult doInBackground(String[] params) {
        if (params.length < 3) {
            return null;
        }

        String arn = params[0];
        String gcmToken = params[1];
        String userData = params[2];

        try {
            CreatePlatformEndpointRequest request = new CreatePlatformEndpointRequest();
            request.setCustomUserData(userData);
            request.setToken(gcmToken);
            request.setPlatformApplicationArn(arn);
            return AWSManager.getSNSClient().createPlatformEndpoint(request);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    protected void onPostExecute(CreatePlatformEndpointResult result) {
        if (!FrameworkUtils.checkIfNull(result)) {
            String endpointArn = result.getEndpointArn();
            SharedPref sharedPref = new SharedPref(mContext, Constants.PREF_FILE_NAME);
            sharedPref.setPref(Constants.NOTIFICATION_ENDPOINT_ARN, endpointArn);
        }
    }

Inside of onPostExecute, the returned CreatePlatformEndpointResult object is null. What am I missing or doing incorrectly to cause this?

portfoliobuilder
  • 7,556
  • 14
  • 76
  • 136

1 Answers1

1

It turns out that the implementation is correct, the arn value was wrong. For others running into this conflict, make sure the information is correct when trying to get the endpointARN. I updated mine to

arn:aws:sns:us-east-1::app/GCM/

This comes from the developer console.

portfoliobuilder
  • 7,556
  • 14
  • 76
  • 136