如何在不通知Android的情况下更新通知?

时间:2018-02-06 19:54:26

标签: android notifications updates

所以我有一个通过MQTT接收温度的应用程序。为了避免被通知发送垃圾邮件,我希望应用程序通知一次,即振动,播放声音然后接下来三次(如果通知未被解除),它将仅更新温度值。所以:

  1. 通知
  2. 更新临时
  3. 更新临时
  4. 更新温度 5(如果你愿意,则为1)通知
  5. 这是我的代码:

     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 个答案:

答案 0 :(得分:1)

您希望致电setOnlyAlertOnce(true),以便更新通知,以免发送声音/振动。