push notification - How to handle token in Android? -


i developing android application , implementing push notifications functionality on it.

i keep on server users tokens , can send them notification when needed.

however , need on how(or more precisely when) send token application server save it.

at moment , in first time applications launches , application asks token , sends token along udid server. because of implementation 1st time application launches token not yet retrieved , empty string sent server! of course 2nd time , on token sent normally. (i register udid , token server every time application launches)

the code looks :

    //registering push     gcmregistrar.checkdevice(this);     gcmregistrar.checkmanifest(this);     final string regid = gcmregistrar.getregistrationid(this);     if (regid.equals("")) {       gcmregistrar.register(this, sender_id);       log.i("****************","i registered!!");     } else {       log.i("****************","already registered");     }      final string android_id = secure.getstring(this.getcontentresolver(),secure.android_id);      string jsonstring = "{\"deviceuuid\":\"" + android_id + "\",\"os\":\"android\", \"pushtoken\":\"" + gcmregistrar.getregistrationid(this) + "\"}";   . . . sending jsonstring server . . .  

as see first time gcmregistrar.getregistrationid(this) null call retrieve new token asychronous , happens after send data server.

the next time application launches token send needs fixed.

one idea register token :

    //registering push     gcmregistrar.checkdevice(this);     gcmregistrar.checkmanifest(this);      gcmregistrar.register(this, sender_id); 

and inside protected void onregistered(context context, string regid) function of gcmintentservice class send token server. way token retrieved before send , sure never null 1st time.

actually how things done in ios app. send token server inside function retrieves , sure going there before try send it.

is approach correct? think?

if using gcm library should register token on server in function

protected void onregistered(context context, string registrationid) 

if not using library have in gcmintentservice

@override public final void onhandleintent(intent intent) {     try {         string action = intent.getaction();         if (action.equals("com.google.android.c2dm.intent.registration")) {             handleregistration(intent);         } else if(action.equals("com.google.android.c2dm.intent.receive")){             handlemessage(intent);         }     } {         synchronized (lock) {             swakelock.release();         }     } } 

where handleregistration() function sends token server.

when send token server , if gets registered can use

gcmregistrar.setregisteredonserver(context, true); 

then can know whether token registered server or not using

gcmregistrar.isregisteredonserver(context); 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -