gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 00:21:26 24 4
gpt4 key购买 nike

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

  • 通知
  • 更新温度
  • 更新温度
  • 更新温度
    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 上进行了测试。

    最佳答案

    您需要调用 setOnlyAlertOnce(true)导致通知更新不发送声音/振动。

    关于android - 如何在不通知的情况下在 Android 中更新通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48650907/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com