NotificationManager.IMPORTANCE_HIGH还是没有声音
NotificationManager.IMPORTANCE_HIGH still no sound
我正在尝试在 android 中创建通知,但无论我设置什么重要性和优先级,都没有声音。我假设声音(铃声)由 android 本身处理,我不需要提供任何 mp3/wav 文件。我正在尝试 android 8.1(实际设备)、8.0(模拟器)和 8.1(模拟器)。在实际设备上创建的通知通道默认关闭声音,我不知道为什么在模拟器上声音是打开的但通知上仍然没有播放声音
这是我的代码:
public void sendMessage(View view) {
createNotificationChannel();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("New Notification")
.setContentText("Lorem Ipsum")
.setPriority(NotificationCompat.PRIORITY_HIGH);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(1, builder.build());
}
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "basic-channel";
String description = "Lorem Ipsum";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
实际设备上的频道
模拟器上的频道
看看这个:
//Define sound URI
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("New Notification")
.setContentText("Lorem Ipsum")
.setPriority(NotificationCompat.PRIORITY_HIGH);
.setSound(soundUri); //This sets the sound to play
构建通知时,需要指明要使用系统默认值:
builder.setDefaults(Notification.DEFAULT_ALL)
并记得清除应用程序数据或重新安装应用程序以正确重新创建通知渠道。通知渠道将保留其初始配置,即使您在代码中重新创建它也是如此。
根本原因:
对于实际设备,这是 OEM 的问题,在我的例子中是小米,我发现这个 link threema.ch/en/faq/notification_channels_xiaomi 说小米为所有应用设置 sound=off 除了 select 少数像 FB 、whatsApp 等
对于模拟器,我们需要完成设置过程,然后通知开始发出声音。
我正在尝试在 android 中创建通知,但无论我设置什么重要性和优先级,都没有声音。我假设声音(铃声)由 android 本身处理,我不需要提供任何 mp3/wav 文件。我正在尝试 android 8.1(实际设备)、8.0(模拟器)和 8.1(模拟器)。在实际设备上创建的通知通道默认关闭声音,我不知道为什么在模拟器上声音是打开的但通知上仍然没有播放声音
这是我的代码:
public void sendMessage(View view) {
createNotificationChannel();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("New Notification")
.setContentText("Lorem Ipsum")
.setPriority(NotificationCompat.PRIORITY_HIGH);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(1, builder.build());
}
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "basic-channel";
String description = "Lorem Ipsum";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
实际设备上的频道
模拟器上的频道
看看这个:
//Define sound URI
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("New Notification")
.setContentText("Lorem Ipsum")
.setPriority(NotificationCompat.PRIORITY_HIGH);
.setSound(soundUri); //This sets the sound to play
构建通知时,需要指明要使用系统默认值:
builder.setDefaults(Notification.DEFAULT_ALL)
并记得清除应用程序数据或重新安装应用程序以正确重新创建通知渠道。通知渠道将保留其初始配置,即使您在代码中重新创建它也是如此。
根本原因: 对于实际设备,这是 OEM 的问题,在我的例子中是小米,我发现这个 link threema.ch/en/faq/notification_channels_xiaomi 说小米为所有应用设置 sound=off 除了 select 少数像 FB 、whatsApp 等
对于模拟器,我们需要完成设置过程,然后通知开始发出声音。