Android - Intent manage. Old intent is resend if user open application from task-manager -
i have problem. when call finish()
method activity still hold in task-manager, , if user restart task-manager activity receive old intent. if intent sent push-notification have unwanted reaction: app start process intent push-notification data.
how correctly manage push-notification intent behavior in activity avoid wrong activity state?
my app receive push-notification , form pending intent reaction on push:
final notificationmanager mnotificationmanager = (notificationmanager) context.getsystemservice(context.notification_service); int defaultoptions = notification.default_lights; defaultoptions |= notification.default_sound; intent intentaction = new intent(context, mainactivity.class); intentaction.setflags(intent.flag_activity_clear_top | intent.flag_activity_single_top); intentaction.putextra(bundle_collapse_key, data.getstring(bundle_collapse_key)); intentaction.setaction(custom_action); final pendingintent pendingintent = pendingintent.getactivity(context, 0, intentaction, pendingintent.flag_update_current); int notificationflags = notification.flag_auto_cancel; final notification notification = new notificationcompat.builder(context).setsmallicon(r.drawable.icon_splash) .setcontenttitle(context.getresources().getstring(r.string.app_name)) .setcontenttext(data.getstring(bundle_push_content_data)) .setcontentintent(pendingintent) .setautocancel(true) .setdefaults(defaultoptions) .getnotification(); notification.flags |= notificationflags; mnotificationmanager.notify(notification_id, notification);
after user came in app push, application, obviously, receive intent custom_action
, work:
private void intentprocess(intent intent) { boolean customaction = intent.getaction().equals(gcmpushreceiver.custom_action); if (customaction) { //push reaction, staff intent } else { //no push reaction, user open activity } }
i call intentprocess
method oncreate
, onnewintent
:
public class mainactivity extends fragmentactivity { //this case if app closed , user tap on push @override protected void oncreate(bundle savedinstancestate) { /* ... */ intentprocess(getintent()); } //this case if app opened , user tap on push @override protected void onnewintent(intent intent) { super.onnewintent(intent); intentprocess(intent); } }
activity declaration in manifest:
<activity android:name=".ui.activity.mainactivity" android:label="@string/app_name" android:screenorientation="portrait" android:windowsoftinputmode="adjustresize"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity>
try use this
int id= notificationid.getid(); final pendingintent pendingintent = pendingintent.getactivity(context, id, intentaction, pendingintent.flag_update_current); // rest of code notificationmanager.notify(id, notification);
add new class
public class notificationid { /** * atomicinteger used in applications such atomically incremented * counters, , cannot used replacement integer. however, * class extend number allow uniform access tools , * utilities deal numerically-based classes. */ private final static atomicinteger c = new atomicinteger(0); /** * method automatically incremented int value. * * @return */ public static int getid() { return c.incrementandget(); }
}
every time notification generated creates new intent different id.
if don't want activity display/added task manager add these lines in activity tag of androidmanifest.
android:excludefromrecents="true" android:nohistory="true"
Comments
Post a Comment