首页 > 解决方案 > Android 通知声音静音

问题描述

我有一个notification我建立的自定义,每次弹出它都会发送一个notification sound. 我希望这是silent并且不发出任何声音。我尝试使用下面的所有内容,但似乎没有任何效果。一切似乎都已被弃用,setPriority()不是一种选择。寻找有关如何使notification声音静音的解决方案,有什么帮助吗?我的代码如下。

尝试使用以下所有内容的组合:

    status.sound = null;
    status.vibrate = null;
    status.defaults &= ~DEFAULT_SOUND;
    status.defaults &= ~DEFAULT_VIBRATE;
    status.vibrate = new long[] { -1 };
    status.priority = -2

我的代码

    Notification status = new NotificationCompat.Builder(this, CHANNEL_1_ID).build();
    status.contentView = views;
    status.bigContentView = bigViews;
    status.flags = Notification.FLAG_ONGOING_EVENT;
    status.icon = R.drawable.ic_music_note;
    status.contentIntent = pendingIntent;
    status.priority = -2;
    startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, status);

标签: androidnotifications

解决方案


要在 API 26 中静音通知声音,请使用IMPORTANCE_LOW不包含任何声音的通道。例子:

 int importance = NotificationManager.IMPORTANCE_LOW;

另外,请务必清除应用数据或重新安装应用,以便在更改重要性后使此更改在通知渠道中生效。

参考文档:迁移 MediaStyle 通知以支持 Android O


推荐阅读