我有一个我构建的自定义通知
,每次弹出时都会发送通知声音
。我希望它安静
并且不发出任何声音。我尝试使用下面的所有内容,但似乎没有任何效果。一切似乎都已被弃用,并且 setPriority()
不是一个选项。正在寻找有关如何使通知
声音静音的解决方案,有帮助吗?我的代码如下。
尝试结合使用以下所有内容:
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);
最佳答案
要在 API 26 中静音通知声音,请使用 IMPORTANCE_LOW
channel ,该 channel 不包含任何声音。示例:
int importance = NotificationManager.IMPORTANCE_LOW;
此外,请确保清除应用数据或重新安装应用,以便在更改重要性后使此更改在通知 channel 中生效。
引用文档:Migrating MediaStyle Notifications to Support Android O
关于Android 通知声音静音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50670902/