即使在应用程序关闭或在后台运行时,我也希望阻止用户关闭Android通知。仅在单击通知时将其关闭(导致应用程序启动并进入前台)。
我目前的情况是,当应用程序处于前台运行时,无法根据需要取消通知。但是,如果应用程序在后台运行或关闭,则滑动时通知会消失。
以下是用于生成通知的代码:
Intent intent = new Intent (this, MainActivity.class);
intent.addFlags (Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity (this, (int) System.currentTimeMillis (), intent, PendingIntent.FLAG_UPDATE_CURRENT);
Builder mBuilder = new Builder (this, channelIDMaxisSystems);
Notification notification = mBuilder.setTicker (title)
.setSmallIcon (R.drawable.icon)
.setWhen (0)
.setAutoCancel (true)
.setContentTitle (title)
.setContentText (content)
.setContentIntent (pendingIntent)
.setOngoing(true)
.build ()
;
notification.flags = notification.flags | Notification.FLAG_AUTO_CANCEL | Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
NotificationManager notificationManager = (NotificationManager) this.getSystemService (Context.NOTIFICATION_SERVICE);
notificationManager.notify (mNotificationId, notification);