如何在不通知的情况下更新通知? [英] How to update a notification without notifying in Android?

查看:88
本文介绍了如何在不通知的情况下更新通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个通过MQTT接收温度的应用程序.为避免通知引起垃圾邮件,我希望该应用程序通知一次,即振动,播放声音,然后在接下来的三遍(如果未取消通知的情况下)仅更新温度值.所以:

So I have an app that receives the temperature via MQTT. To avoid getting spammed by notifcations, I want the app to notify once, that is vibrate, play sound and then the next three times (if the notification isn't dismissed) it will only update the temperature value. So:

  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上进行了测试.

With this code, I always get, what it looks like, a brand new notification with sound and vibration. I've looked at previous questions regarding this and they all say that it's important to use the same builder and as you can see, I have done this. Is there some change in newer Android versions that I'm not aware of? I've tested on both 7.1.1 and 8.1.

推荐答案

您将要致电

You'll want to call setOnlyAlertOnce(true) to cause updates to your notification to not send sound/vibration.

这篇关于如何在不通知的情况下更新通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆