在我的应用中,当在异步文件中完成任务时,它会显示包含此代码的通知
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent pIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, new Intent(), 0);
Notification noti = new Notification.Builder(context)
.setContentTitle("Complete")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.setAutoCancel(true)
.build();
notificationManager.notify(0, noti);
问题是当我点击通知时,没有任何反应。基本上我想要它,如果应用程序已经打开并且用户点击通知,那么什么都不应该打开。如果应用程序未打开(这意味着它仍在运行但最小化),那么我希望它打开应用程序,就像最大化它一样。
有谁知道怎么做?
由于
答案 0 :(得分:4)
您需要一个Intent
示例代码:
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
这将打开您的MainActivity