如何在Android中更新通知而不进行通知?

3
我有一个应用程序通过MQTT接收温度。为了避免被繁琐的通知骚扰,我希望该应用程序只通知一次,即振动、播放声音,然后在下面的三次更新中(如果未解除通知),仅更新温度值。所以:

  1. 通知
  2. 更新温度
  3. 更新温度
  4. 更新温度 5(或1)次通知

这是我的代码:

 private final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, id);

 private void handleNotification(String message, String topic) {
        if (notifManager == null) {
            notifManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        }

        if (isNotifRunning() && ticker < 3) {
            updateNotif(message);
            ticker++;
        } else {
            createNotif(message);
            ticker = 0;
        }
    }

    private void createNotif(String message) {

        Intent intent;
        PendingIntent pendingIntent;


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel mChannel = notifManager.getNotificationChannel(id);
            if (mChannel == null) {
                mChannel = new NotificationChannel(id, name, importance);
                mChannel.setDescription(description);
                mChannel.enableVibration(true);
                mChannel.setVibrationPattern(new long[]{100, 200});
                notifManager.createNotificationChannel(mChannel);
            }
            intent = new Intent(this, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

            builder.setContentTitle(getString(R.string.notifTmpLowTitle))
                    .setSmallIcon(R.drawable.ic_ac_unit_black_24px)
                    .setContentText(getString(R.string.notifTmpLowText) + " " + message + Constants.CELSIUS)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent)
                    .setTicker(message)
                    .setColor(getResources().getColor(R.color.colorCold))
                    .setVibrate(new long[]{100, 200});

        } else {

            intent = new Intent(this, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

            builder.setContentTitle(getString(R.string.notifTmpLowTitle))
                    .setSmallIcon(R.drawable.ic_ac_unit_black_24px)
                    .setContentText(getString(R.string.notifTmpLowText) + " " + message + Constants.CELSIUS)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent)
                    .setTicker(message)
                    .setVibrate(new long[]{100, 200})
                    .setColor(getResources().getColor(R.color.colorCold))
                    .setPriority(Notification.PRIORITY_HIGH);
        }
        Notification notification = builder.build();
        notifManager.notify(NOTIFY_ID, notification);
    }

    //TODO Doesn't update, posts a new notification.
    private void updateNotif(String message) {
        builder.setContentText(getString(R.string.notifTmpLowText) + " " + message + Constants.CELSIUS);
        Notification notification = builder.build();
        notification.flags = Notification.FLAG_ONGOING_EVENT;
        notifManager.notify(NOTIFY_ID, notification);
    }
    //---------------------------------------------

使用这个代码,我总是获得一个看起来像是全新的带有声音和振动的通知。我查看了之前关于这个问题的提问,他们都说使用相同的构建器很重要,正如你所看到的,我已经这样做了。是否在新版本的Android中有一些我不知道的更改?我已经在7.1.1和8.1上进行了测试。
1个回答

5

太棒了,这正是我正在寻找的! - kodde45
只是一个奇怪的问题。现在通知被触发的时间从不改变。即使我取消它并创建一个新的,时间只会增加。现在已经过去了19个小时。我说的是你可以通过使用builder.setShowWhen(bool)来启用或禁用的时间属性。 - kodde45
如果你正在重复使用同一个构建器,如果你想要更新时间(默认为构建器首次创建的时间),你应该调用setWhen(System.currentTimeMillis()) - ianhanniballake

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接