android - questions about switching from C2DM to GCM -
i'm converting app c2dm gcm , have questions.
i've modified gcmquickstart example found here:
git clone https://github.com/googlesamples/google-services.git
in class extends application, i've got:
public class myapp extends application { @override public void oncreate() { super.oncreate(); if (this.checkplayservices()) { intent intent = new intent(this, registrationintentservice.class); startservice(intent); } else{ log.e(tag,"failed checkforgcm"); } } }
afaik, starts service , it'll run forever or if operating system shuts down whereupon operating system restart it. correct?
checkplayservices() makes sure android 2.3 or greater , google play services installed these requirements use gcm. correct?
public class registrationintentservice extends intentservice{ private static final string tag = "regintentservice"; public registrationintentservice() { super(tag); } @override protected void onhandleintent(intent intent) { try { // in (unlikely) event multiple refresh operations // occur simultaneously, // ensure processed sequentially. synchronized (tag) { // [start register_for_gcm] // call goes out network // retrieve token, // subsequent calls local. // [start get_token] instanceid instanceid = instanceid.getinstance(this); string token = instanceid.gettoken(getstring(r.string.googleprojectid), googlecloudmessaging.instance_id_scope, null); log.d(tag, "gcm registration token: " + token); /* store token */ // [end register_for_gcm] } } catch (exception e) { log.e(tag, "failed complete token refresh", e); } }
}
what "registers gcm"? this:
instanceid instanceid = instanceid.getinstance(this);
what "gets token"? this:
string token = instanceid.gettoken(getstring(r.string.googleprojectid), googlecloudmessaging.instance_id_scope, null);
i'm little troubled talk of refresh operations. specifically, have refresh token periodically? if where, how , 1 (instanceid instanceid or string token) ?
afaik, gcm send me new token when appropriate. i've got ready accept , store @ time. right?
using registrationintentservice shown above allows me accept/store token @ time. right?
the synchronization bit troubling. seems imply google send me multiple registration tokens. there way trigger can see larger impact on app?
also "subsequent calls local" ? calls token i've received here , stored on device?
public class myinstanceidlistenerservice extends instanceidlistenerservice { private static final string tag = "myinstanceidls"; /** * called if instanceid token updated. may occur if security of * previous token had been compromised. call initiated * instanceid provider. */ // [start refresh_token] @override public void ontokenrefresh() { // fetch updated instance id token , notify our app's server of changes (if applicable). intent intent = new intent(this, registrationintentservice.class); startservice(intent); } // [end refresh_token] }
this confused. based on reading code, think idea if gcm server decides need instanceid communicate necessity myinstanceidlistenerservice. @ point, myinstanceidlistenerservice cause registrationintentservice run , new instanceid , token. correct?
/** intent service handle each incoming gcm message */ public class mygcmlistenerservice extends gcmlistenerservice { final static string tag = "gcmservice"; /** * called when message received. * * @param senderid of sender. * @param data data bundle containing message data key/value pairs. * set of keys use data.keyset(). */ // [start receive_message] @override public void onmessagereceived(string from, bundle data) { //do stuff data communicated server app //via gcm registration token received in //registrationintentservice. onhandleintent(intent intent) } // [end receive_message] }
this service understandable me. whole point of exercise have gcm assign token app, app communicate token 1 of our servers, our server sends message gcm telling "send guy token" , reaches app in above code. correct?
here's androidmanifest puts together:
<!-- [start gcm_receiver] --> <receiver android:name="com.google.android.gms.gcm.gcmreceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.send" > <intent-filter> <action android:name="com.google.android.c2dm.intent.receive" /> <category android:name="my.package" /> </intent-filter> </receiver> <!-- [end gcm_receiver] --> <!-- [start gcm_listener] --> <service android:name="my.package.mygcmlistenerservice" android:exported="false" > <intent-filter> <action android:name="com.google.android.c2dm.intent.receive" /> </intent-filter> </service> <!-- [end gcm_listener] --> <service android:name="my.package.myinstanceidlistenerservice" android:exported="false"> <intent-filter> <action android:name="com.google.android.gms.iid.instanceid"/> </intent-filter> </service> <service android:name="my.package.registrationintentservice" android:exported="false"> </service>
why have explicitly start registrationintentservice in myapp with:
intent intent = new intent(this, registrationintentservice.class); startservice(intent);
but not other services?
if (this.checkplayservices()) { intent intent = new intent(this, registrationintentservice.class); startservice(intent); }
this starts service , it'll run forever or if operating system shuts down whereupon operating system restart it. correct?
no. intentservice exist until completed code in onhandleintent
checkplayservices()
makes sure android 2.3 or greater , google play services installed these requirements use gcm. correct?
yeah. , no. need network connection. check network before checking play services.
what "registers gcm"? this:
instanceid instanceid = instanceid.getinstance(this);
what "gets token"? this:
string token = instanceid.gettoken(getstring(r.string.googleprojectid), googlecloudmessaging.instance_id_scope, null);
yes. getinstance()
register gcm. gettoken
give token registered user.
this service understandable me. whole point of exercise have gcm assign token app, app communicate token 1 of our servers, our server sends message gcm telling "send guy token" , reaches app in above code. correct?
yeah. pretty much.
why have explicitly start registrationintentservice in myapp with:
intent intent = new intent(this, registrationintentservice.class); startservice(intent);
but not other services?
you declare instanceidlistenerservice
, gcmlistenerservice
in manifest because need run time. if not registered, wont anything. registrationservice
not called in manifest because may not want start immediately. instance, if want have user register app, once authenticated register gcm starting service.
Comments
Post a Comment