我使用此代码创建了一个通知:
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
final int notificationID = NotificationID.getID();
PendingIntent pendingIntent = PendingIntent.getActivity(this, notificationID, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notif_icon)
.setContentTitle(messageTitle)
.setStyle(new NotificationCompat.BigTextStyle().bigText(messageTitle + "n" +messageBody))
.setContentText(messageTitle + " " +messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationID, notificationBuilder.build());
这在许多设备上都可以很好地工作,但是在三星Galaxy S6 Android 7上,通知立即被取消。
我尝试将正在进行的标志放置,并且该通知仍然可见,但是这不是解决方案,因为用户将无法取消通知。
知道什么可能导致问题?
从您的代码中删除setautocancel(true)。
setautocancel()
Notification.builder setautocancel(布尔式autocancel) 当用户触摸它时,通知会自动删除。
参考:https://developer.android.com/reference/android/app/notification.builder.html#setautocancel(boolean)