【发布时间】:2019-11-03 16:26:18
【问题描述】:
我正在尝试在 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);
}
}
实际设备上的频道
模拟器上的频道
【问题讨论】:
标签: android notifications