首页 > 解决方案 > 如何用新收到的通知替换旧通知,而不是在通知托盘中创建一堆通知?

问题描述

我在通知面板中遇到通知问题。如果在应用程序处于后台时收到新通知时通知中存在旧通知,我需要清除旧通知。我能做什么或如何用新通知替换旧通知。或者,如果应用程序在后台或被杀死,是否有可能根据收到的通知启动应用程序/活动?这是我的通知代码。

 NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setContentTitle(title);
        builder.setContentText(message);
        builder.setAutoCancel(true);

        builder.setLights(ContextCompat.getColor(this, R.color.colorPrimary), 50, 50);
        builder.setSmallIcon(R.drawable.notification);
      builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
        builder.setVibrate(new long[]{500, 500});

        Intent intent = new Intent(this, LauncherActivity.class);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            builder.setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(notification_id, builder.build());

标签: javaandroidkotlin

解决方案


只有notificationID,一样

 notificationManager.notify(0, builder.build());

推荐阅读