1

我使用以下代码创建了一个通知:

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 上,通知会立即取消。

我尝试放置正在进行的标志并且通知仍然可见,但这不是解决方案,因为用户将无法通过滑动通知来取消它。

知道什么可能导致问题吗?

4

1 回答 1

1

从您的代码中删除 setAutoCancel(true)。

设置自动取消()

Notification.Builder setAutoCancel (boolean autoCancel) 使该通知在用户触摸时自动关闭。

参考:https://developer.android.com/reference/android/app/Notification.Builder.html#setAutoCancel(boolean)

于 2017-09-01T18:50:22.617 回答