Registration Tips

I have an application with login / logout that uses Android accounts. I also use GCM, so when the user logs in, I register the device, and if the user logs out, I cancel it. I ran into a problem with an unregistered call.

To unregister my device from my server, I need an account token. Since I cannot pass it to the onUnregistered method from GCMIntentService (or at least I don’t know how to do this), I tried to get it using the AccountManager, but at this point my account has already been deleted. I want to solve this, and it occurred to me two options:

1- Call GCMRegistrar.unregister first and delete the account in the GCMIntentService-> onUnregistered function. The problem with this solution is that the user has to wait for the registration to complete, and maybe not. I cannot allow the user to continue working without deleting the account.

2- Save the token somewhere (maybe the application class) to get it from GCMIntentService-> onUnregistered.

I think number 2 is better, but I don't know if it is the best. Is there a better way to solve it?

+4
source share
2 answers

Once you get the token in the IntentService, you can save it using SharedPreferences:

public void savePrefrences(String key, String value) { SharedPreferences prefs = SynergyApplication._context.getSharedPreferences(SynergyApplication._context.getApplicationContext().getPackageName(), 0); prefs.edit().putString(key, value).commit(); } 

And then read it everywhere in your application:

 public String getPrefrences(String key) { SharedPreferences prefs = SynergyApplication._context.getSharedPreferences(SynergyApplication._context.getApplicationContext().getPackageName(), 0); return prefs.getString(key, ""); } 
+2
source

You can use onDestroy in your main business. In this case, you must call this method when the user closes the application. I do not know how you remove the device from your server (with the URL?).

The onDestroy method ensures that your code will be executed when the user closes the application.

0
source

Source: https://habr.com/ru/post/1436181/


All Articles