在系统上,我的应用程序的通知被静音。默认情况下,我如何允许我的应用程序播放通知声音?所有手机都有
通常以下代码控制通知声音
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "name", NotificationManager.IMPORTANCE_LOW);
重要性低意味着通知被静音。高重要性通知通知用户。
但是,如果Android系统禁用应用程序的声音,您可能无法播放通知的声音。
在Oreo之前的设备上,您可以通过将优先级设置为高来启用通知声音
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setPriority(NotificationCompat.PRIORITY_HIGH) // Set priority to PRIORITY_HIGH to enable notification sound
.setContentIntent(pendingIntent)
.setAutoCancel(true);
在奥利奥及以上设备上,您必须为通知创建一个通道
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Set importance to IMPORTANCE_HIGH to enable notification sound on Android 8.0 and above
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "channel name", NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}