Android - Having trouble with calling a service from an action button of a notification -
Android - Having trouble with calling a service from an action button of a notification -
i want able have button copies text notification clipboard. notification sent through google's gcm service.
the first time notification arrives when press "copy" button fine , text goes clipboard service button sends intent to. sec time notification arrives different text when press "copy" button content of first notification goes clipboard instead of new one. when debug code, seems intent that's calling service has new content, service puts clipboard runs parameters of old notification, if same session of service awaken old intent.
any clue why happening?
// called gcm notification handler private void sendnotification(string msg) { mnotificationmanager = (notificationmanager) this.getsystemservice(context.notification_service); pendingintent contentintent = pendingintent.getactivity(this, 0,new intent(this, mainactivity.class), 0); intent notificationintent = new intent(this, clipboardservice.class); notificationintent.putextra("tool",msg); pendingintent serviceintent = pendingintent.getservice(this, 0, notificationintent, 0); notificationcompat.builder mbuilder = new notificationcompat.builder(this) .setsmallicon(r.drawable.ic_stat_gcm) .setcontenttitle("here's text!") .setstyle(new notificationcompat.bigtextstyle() .bigtext(msg)) .setcontenttext(msg) .addaction(r.drawable.ic_stat_gcm, "copy", serviceintent); // <--- intent have right (new) value on sec run mbuilder.setcontentintent(contentintent); mnotificationmanager.notify(notification_id, mbuilder.build()); }
this service notification actions calls to:
public class clipboardservice extends intentservice { public clipboardservice() { super("clipboardservice"); } @override protected void onhandleintent(intent intent) { //this intent have values of first intent fire, instead of updated one. string msg = (string) intent.getextras().get("tool"); clipboardmanager clipboard = (clipboardmanager) getsystemservice(context.clipboard_service); clipdata clip = clipdata.newplaintext("2android",msg); clipboard.setprimaryclip(clip); }
although late reply question, faced same issue , google led me unanswered question. here understanding if in future dig up.
this because pendingintent
uses cached intent extras previous instance of intent unless explicitly tell not to. there 2 ways that...
use flag_cancel_current
or flag_update_current
(best in case) flag while constructing pendingintent
. first flag automatically dismiss previous pendingintent
, create new 1 , sec 1 update extras pendingintent
, save overhead of creating new one.
pendingintent.getservice(this, 0, notificationintent, flag_cancel_current | flag_update_current);
pass unique requestcode
pendingintent
constructor everytime. generate unique pending intents everytime can access extras associated particular intent later. believe not required in case.
pendingintent.getservice(this, unique_id, pi, 0);
android android-notifications google-cloud-messaging intentservice
Comments
Post a Comment