Frage

Please any one advice me on my logical doubt to my android application.

My application doesn't require a registration for using. But I need to send pushnotification ( say GCM ) to all the users. So that I made a database table as follows

user_id      |     gcm_regid      
-------------+-----------------------------------------------
1            |   ( reg id get from google at register() )
-------------+-----------------------------------------------
2            |   ( reg id get from google at register() )
-------------+-----------------------------------------------

So the logic as follows

enter image description here

Now the problem is

Case 1 : When the user "clear the data" in their phone and relaunch the app, another registration process will occur. As a result, when we send push notification, user will get multiple times ( Since the last gcmregid present in our db )

Since its a simple news application, no need for user registration. So anyone please give me a solution for this ?

Thanks in advance

War es hilfreich?

Lösung

The problem of a user clearing the application data (or uninstalling and installing the app) can be quite reliably solved by storing data in the external storage of the device. If data is not available in shared preferences, try to locate it in a file stored in the external storage. Such file will not be deleted when the app is uninstalled. It can still be removed manually by the user though, but there's nothing you can do about that.

Most of the time, registering to GCM multiple times on the same device will return the same registration ID, so your server won't have multiple registration IDs for the same device (assuming it checks for existence of registration ID before updating the DB).

If the registration ID for the same device changes, you can detect it by creating a unique instance identifier for each device, and storing that identifier with the registration ID in your DB. When you send that instance identifier + a registration ID to your server, first check if that instance identifier exists in your DB. If it exists, replace the old registration ID (associated with that instance identifier) by the new registration ID. You should store the instance identifier in the external storage of the device.

Andere Tipps

Are you creating same registration ID for specific device (On Client Side) ?
If So,Do following

On Server side first check if registration ID is present in database then update its information such as updateTime else register as a new ID. In this case your server will send per message per device.

else
if registration ids are different on each registration process,then gcm will handle itself,i.e. it will not send message to old registration ID after some time.(In this case device will get only one message since it having only unique one registration ID)

Edit

On Device Re-Registration with GCM,most of the time it create same registration id,So you check IDs duplication on server side as I did mentioned above.

And Sometimes Google changes the registration ID and you'll have multiple IDs associated with one device(still google/GCM send only one notification to device because GCM server send message to new ID of that device),In this case check for canonical IDs. For more info about see Canonical IDs

here the code this will solve your problem.....

run this code in splash screen.....

class GCMRegisterAsync extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... params) {

            GCMRegistrar.checkDevice(yourclass.this);
            GCMRegistrar.checkManifest(yourclass.this);
            final String regId = GCMRegistrar
                    .getRegistrationId(yourclass.this);
            if (regId.equals("")) {
                GCMRegistrar.register(yourclass.this, "xxxxxxxxxxxx");
            } else {
                Log.d("test", "Already registered");
            }

            GCMRegistrar.checkDevice(yourclass.this);
            Log.d("info",
                    "unregistereddd....."
                            + GCMRegistrar.getRegistrationId(yourclass.this));
            GCMRegistrar.checkManifest(yourclass.this);
            if (GCMRegistrar.isRegistered(yourclass.this)) {
                Log.e("info gcm id is ",
                        GCMRegistrar.getRegistrationId(yourclass.this));
            }

        secondclass.regId = GCMRegistrar
                    .getRegistrationId(yourclass.this);
            if (RegisterHalf.regId.equals("")) {
                // replace this with the project ID
                GCMRegistrar.register(yourclass.this, "xxxxxxxxxxxx");
                Log.e("reg id is ",
                        GCMRegistrar.getRegistrationId(yourclass.this)
                        );


            } else {
                Log.e("info", "already registered as" + RegisterHalf.regId);
            }

            return null;
        }
    }

after this paste the code where you are the saving the id to the database..

SharedPreferences pref = getSharedPreferences(
                        com.omega.expert.classes.SharedPrefConstants.pref_name, MODE_PRIVATE);
                GCMRegistrar.checkDevice(yourclass.this);
                GCMRegistrar.checkManifest(yourclass.this);

                regId = GCMRegistrar.getRegistrationId(yourclass.this);
                Log.e("test", "Reg id:" + regId);

                pref.edit().putString(com.omega.expert.classes.SharedPrefConstants.key_gcmid, regId)
                        .commit();

regId is your device id... save to database ....

i have successfully implemented. this your multiple device id problem will be solved...

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top