Android通知频道使用新通知推送较旧的未读通知

时间:2018-08-09 08:27:27

标签: android push-notification firebase-cloud-messaging android-8.0-oreo notification-channel

我是Android通知频道的新手。我遵循了Google Dev文档中的指南,但是在推送通知时遇到了一些麻烦。我的问题是,如果我收到通知并轻扫取消或只是不单击它,则以下通知与较旧的轻扫已取消或未更改的通知一起提供。通知正在累积增加。

我的代码如下:

   A  B
0  1  NaN
1  1  3
2  4  NaN

模块摇篮:

NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    String channelId = "some_channel_id";
    CharSequence channelName = "Some Channel";
    int importance = NotificationManager.IMPORTANCE_LOW;
    NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
    notificationChannel.enableLights(true);
    notificationChannel.setLightColor(Color.RED);
    notificationChannel.enableVibration(true);
    notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
    notificationManager.createNotificationChannel(notificationChannel);


    int num = (int) System.currentTimeMillis();

    Notification notification = new Notification.Builder(this)
            .setContentTitle("Some Message")
            .setContentText("You've received new messages!")
            .setAutoCancel(true)
            .setSmallIcon(R.drawable.fav_ico)
            .setChannelId(channelId)
            .build();

    notificationManager.notify(num, notification);

我在哪里犯这个错误?

1 个答案:

答案 0 :(得分:0)

尝试此代码

        int importance = NotificationManager.IMPORTANCE_DEFAULT;
    NotificationChannel mChannel = new NotificationChannel(LOCATION_CHANNEL, LOCATION_NAME, importance);
    mChannel.enableLights(false);
    mChannel.enableVibration(false);

    int id = getApplicationInfo().icon;

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, mChannel.getId())
            .setSmallIcon(id)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(getString(R.string.location_notification_text))
            .setPriority(NotificationManager.IMPORTANCE_DEFAULT)
            .setVibrate(new long[] {0L})
            .setChannelId(mChannel.getId())
            .setAutoCancel(true);
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(pendingIntent);
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (manager != null) {
        manager.createNotificationChannel(mChannel);
        manager.notify(NOTIFICATION_ID, mBuilder.build());
    }