简体   繁体   中英

make notification when clicking app bring to front

I want make a notification, that when clicked on it will bring my app from the background to the front. I am using the following code:

NotificationManager noma = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PendingIntent pen = PendingIntent.getActivity(Timer.this, 0, intent, 0);
intent.putExtra("key", "trigerred");
String body = "This is a message";
String title = "This is title";
Notification no = new Notification(R.drawable.ic_launcher, body, System.currentTimeMillis());
no.defaults = Notification.DEFAULT_ALL;
no.setLatestEventInfo(this, title, body, pen);
noma.notify(uniqueID, no);

When I click on the notification that makes a new intent but I want the last created intent brought to the front. How i can do this?

You need to set the FLAG_ACTIVITY_SINGLE_TOP flag on the intent that you pass to getActivity. This should bring you back to the all ready running activity when clicking your notification.

See here for a list of the different launch flags.

尝试这个

PendingIntent pen = PendingIntent.getActivity(Timer.this, 0, intent,Intent.FLAG_ACTIVITY_TASK_ON_HOME);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM