【发布时间】:2015-07-15 11:33:17
【问题描述】:
当有新消息到达时,我的应用会推送通知。一切正常。当用户点击通知时,它按预期恢复Activity,通知被取消(删除)。
但是,当用户重新打开应用(不是通过通知)时,通知仍然保留在状态栏中。
所以,我想取消Activity 的onResume() 方法内的通知。
这是创建通知时的代码:
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
.setContentTitle(channel + ": Unread messages")
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)
.setContentIntent(intent)
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND | Notification.FLAG_AUTO_CANCEL);
notificationManager.notify(0, notification.build());
【问题讨论】:
标签: android android-intent notifications android-pendingintent