Oreo(API> = 26)仅显示最后通知

时间:2018-06-12 16:32:12

标签: java android android-notifications

我有简单的代码来显示通知。我们可以说:

public void onMessageReceived(RemoteMessage remoteMessage) {
    final PushNotification pushNotification = fromData(remoteMessage.getData());
    final String OPTIMUS_CHANNEL = "XXX";
    final int notificationId = (int) (System.currentTimeMillis() % Integer.MAX_VALUE);

    final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, OPTIMUS_CHANNEL)
            .setGroup(GROUP_KEY)
            .setContentIntent(preparePendingIntent(notificationId, pushNotification))
            .setWhen(pushNotification.getTime())
            .setSmallIcon(getIconId(pushNotification))
            .setLargeIcon(getIcon(pushNotification))
            .setContentTitle(pushNotification.getTitle())
            .setContentText(pushNotification.getMessage());

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationManager notificationManager = getSystemService(NotificationManager.class);

        if(notificationManager == null) {
            throw new RuntimeException("Can't load notification manager.");
        }

        NotificationChannel channel = new NotificationChannel(OPTIMUS_CHANNEL, OPTIMUS_CHANNEL, NotificationManager.IMPORTANCE_HIGH);
        channel.enableLights(true);
        channel.setLightColor(getLightColor(pushNotification));

        notificationManager.createNotificationChannel(channel);
    }

    Notification notification = builder.build();

    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(notificationId, notification);
}

效果很好。发送通知时一切正常。我在使用Android 8.0的设备上测试时出现问题。

如果我向设备发送多个通知,则会发送一个以上的通知:> = 26我只能看到最后发送的通知(之前的通知已消失)。

在较旧的设备上按预期工作。

有人遇到过类似的问题吗?

祝你好运, 阿德里安

0 个答案:

没有答案