google cloud messaging - Android GCM example - how to implement in existing project? -
i've downloaded latest gcm example fromm google on github.
it works well, can't make work in own project. i've got donut turning , message "generating instanceid token..."
i didn't understand link class gcmsender in example.
here code
public class mainactivity extends actionbaractivity { private static final int play_services_resolution_request = 9000; private static final string tag = "mainactivity"; private broadcastreceiver mregistrationbroadcastreceiver; private progressbar mregistrationprogressbar; private textview minformationtextview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // url string url="http://www.monurl.org/"; webview mwview=(webview) this.findviewbyid(r.id.webview); //autorise javascript mwview.getsettings().setjavascriptenabled(true); mwview.loadurl(url); //partie notif mregistrationprogressbar = (progressbar) findviewbyid(r.id.registrationprogressbar); mregistrationbroadcastreceiver = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { mregistrationprogressbar.setvisibility(progressbar.gone); sharedpreferences sharedpreferences = preferencemanager.getdefaultsharedpreferences(context); boolean senttoken = sharedpreferences .getboolean(quickstartpreferences.sent_token_to_server, false); if (senttoken) { minformationtextview.settext(getstring(r.string.gcm_send_message)); } else { minformationtextview.settext(getstring(r.string.token_error_message)); } } }; minformationtextview = (textview) findviewbyid(r.id.informationtextview); // check si google play services apk est sur le phone if (checkplayservices()) { // start intentservice register application gcm. intent intent = new intent(this, mygcmregistrationintentservice.class); startservice(intent); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } /** * check device make sure has google play services apk. if * doesn't, display dialog allows users download apk * google play store or enable in device's system settings. */ private boolean checkplayservices() { int resultcode = googleplayservicesutil.isgoogleplayservicesavailable(this); if (resultcode != connectionresult.success) { if (googleplayservicesutil.isuserrecoverableerror(resultcode)) { googleplayservicesutil.geterrordialog(resultcode, this, play_services_resolution_request).show(); } else { log.i(tag, "ce peripherique n'est pas supporte."); finish(); } return false; } return true; } }
public class mygcmlistenerservice extends gcmlistenerservice { private static final string tag = "mygcmlistenerservice"; public mygcmlistenerservice() { } /** * 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) { string message = data.getstring("message"); log.d(tag, "from: " + from); log.d(tag, "message: " + message); /** * production applications process message here. * eg: - syncing server. * - store message in local database. * - update ui. */ /** * in cases may useful show notification indicating user * message received. */ sendnotification(message); } // [end receive_message] /** * create , show simple notification containing received gcm message. * * @param message gcm message received. */ private void sendnotification(string message) { intent intent = new intent(this, mainactivity.class); intent.addflags(intent.flag_activity_clear_top); pendingintent pendingintent = pendingintent.getactivity(this, 0 /* request code */, intent, pendingintent.flag_one_shot); uri defaultsounduri= ringtonemanager.getdefaulturi(ringtonemanager.type_notification); notificationcompat.builder notificationbuilder = new notificationcompat.builder(this) .setsmallicon(r.drawable.ic_stat_ic_notification) .setcontenttitle("gcm message") .setcontenttext(message) .setautocancel(true) .setsound(defaultsounduri) .setcontentintent(pendingintent); notificationmanager notificationmanager = (notificationmanager) getsystemservice(context.notification_service); notificationmanager.notify(0 /* id of notification */, notificationbuilder.build()); log.v("notification message",message); } }
public class mygcmregistrationintentservice extends intentservice { private static final string tag = "mygcmregistrationintentservice"; private static final string[] topics = {"global"}; /** * starts service perform action foo given parameters. if * service performing task action queued. * * @see intentservice */ // todo: customize helper method public static void startactionfoo(context context, string param1, string param2) { intent intent = new intent(context, mygcmregistrationintentservice.class); intent.setaction(action_foo); intent.putextra(extra_param1, param1); intent.putextra(extra_param2, param2); context.startservice(intent); } /** * starts service perform action baz given parameters. if * service performing task action queued. * * @see intentservice */ // todo: customize helper method public static void startactionbaz(context context, string param1, string param2) { intent intent = new intent(context, mygcmregistrationintentservice.class); intent.setaction(action_baz); intent.putextra(extra_param1, param1); intent.putextra(extra_param2, param2); context.startservice(intent); } public mygcmregistrationintentservice() { // super("mygcmregistrationintentservice"); // ajout super(tag); //fin ajout } @override protected void onhandleintent(intent intent) { sharedpreferences sharedpreferences = preferencemanager.getdefaultsharedpreferences(this); 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.gcm_defaultsenderid), googlecloudmessaging.instance_id_scope, null); // [end get_token] log.i(tag, "gcm registration token: " + token); // todo: implement method send registration app's servers. sendregistrationtoserver(token); // subscribe topic channels subscribetopics(token); // should store boolean indicates whether generated token has been // sent server. if boolean false, send token server, // otherwise server should have received token. sharedpreferences.edit().putboolean(quickstartpreferences.sent_token_to_server, true).apply(); // [end register_for_gcm] } } catch (exception e) { log.d(tag, "failed complete token refresh", e); // if exception happens while fetching new token or updating our registration data // on third-party server, ensures we'll attempt update @ later time. sharedpreferences.edit().putboolean(quickstartpreferences.sent_token_to_server, false).apply(); } // notify ui registration has completed, progress indicator can hidden. intent registrationcomplete = new intent(quickstartpreferences.registration_complete); localbroadcastmanager.getinstance(this).sendbroadcast(registrationcomplete); } /** * handle action foo in provided background thread provided * parameters. */ private void handleactionfoo(string param1, string param2) { // todo: handle action foo throw new unsupportedoperationexception("not yet implemented"); } /** * handle action baz in provided background thread provided * parameters. */ private void handleactionbaz(string param1, string param2) { // todo: handle action baz throw new unsupportedoperationexception("not yet implemented"); } private void subscribetopics(string token) throws ioexception { (string topic : topics) { gcmpubsub pubsub = gcmpubsub.getinstance(this); pubsub.subscribe(token, "/topics/" + topic, null); } } /** * persist registration third-party servers. * * modify method associate user's gcm registration token server-side account * maintained application. * * @param token new token. */ private void sendregistrationtoserver(string token) { // add custom implementation, needed. } private void sendtokentoserver(string token) { } }
public class myinstanceidlistenerservice extends instanceidlistenerservice { public myinstanceidlistenerservice() { } @override public ibinder onbind(intent intent) { // todo: return communication channel service. throw new unsupportedoperationexception("not yet implemented"); } // pour les notifs @override public void ontokenrefresh() { // fetch updated instance id token , notify our app's server of changes (if applicable). intent intent = new intent(this, mygcmregistrationintentservice.class); startservice(intent); } }
hello found happened, i've forgotten in main
@override protected void onresume() { super.onresume(); localbroadcastmanager.getinstance(this).registerreceiver(mregistrationbroadcastreceiver, new intentfilter(quickstartpreferences.registration_complete)); } @override protected void onpause() { localbroadcastmanager.getinstance(this).unregisterreceiver(mregistrationbroadcastreceiver); super.onpause(); }
Comments
Post a Comment