Search code examples
androidgoogle-cloud-messagingmessagingmessageamazon-sns

Duplicate Google cloud message account registered when app re-install


I am working on integrate GCM to the android app, my working flow is first whenever the user enter the app, it check whether the share pref. has the gcm id, if not , then reg and save to share pref.

The problem is, it seems the id is duplicate after I install the app again. For example, if I install three times, then the app will receive three times of the message.

As I am using Amazon SNS service , the GCM id is store in their database, how can I handle the case of user re-install the app to prevent register again?

Thanks

if (gs.settings.getString("endpoint_arn", "").equals("")) {
    gcm = GoogleCloudMessaging.getInstance(ctx);
    asnsc = new AmazonSNSClient(new BasicAWSCredentials(Constant.id,Constant.secret));
    asnsc.setRegion(Region.getRegion(Regions.AP_SOUTHEAST_1));
    asnsc.setEndpoint("sns.ap-southeast-1.amazonaws.com");

    new AsyncTask() {
        @Override
        protected Object doInBackground(final Object... params) {
            try {
                CreatePlatformEndpointRequest per = new CreatePlatformEndpointRequest();
                per.setToken(gcm.register(Constant.projectID));
                per.setPlatformApplicationArn(Constant.platformARN);
                CreatePlatformEndpointResult result = asnsc.createPlatformEndpoint(per);
                String arn = result.getEndpointArn();
                gs.editor.putString("endpoint_arn", arn).commit();
     }
}

Solution

  • You have to check for these 3 steps in order to set up everything working:

    • Firstly unregister the old registration id before you register the new id as mentioned in the comments.
    • Secondly, when you unregister, you should send the old registration ID to your server and remove it from your database.
    • If after registering and getting the new registration ID you send a message with the old registration ID, Google will respond you with a new registration id. This response indicates that your server should delete the old registration ID and use only the new one.