How to update a notification without notifying in Android?

家住魔仙堡 提交于 2019-12-02 16:29:30

问题


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. Notify
  2. Update temp
  3. Update temp
  4. Update temp 5(or 1 if you will) Notify

This is my code:

 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);
    }
    //---------------------------------------------

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.


回答1:


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



来源:https://stackoverflow.com/questions/48650907/how-to-update-a-notification-without-notifying-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!