Android 通知频道声音不工作
Android notification channel sound doesn't work
我不知道为什么,但无论我如何更改原始文件中的 mp3 声音,通知只会发出一种声音,它的名称是最少的字母,例如:My raw file,通知将播放message_tone 无论我在代码中更改什么名称:
/// Set sound for channel
notificationChannel.setSound(Uri.parse("android.resource://" + getPackageName() + '/' + R.raw.sneeze), null);
我的频道设置代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
/// Set channel's ID, name, and importance
NotificationChannel notificationChannel = new NotificationChannel(
CHANNEL_TEST,
"OUTFITTERX",
NotificationManager.IMPORTANCE_HIGH
);
/// Set sound for channel
notificationChannel.setSound(Uri.parse("android.resource://" + getPackageName() + '/' + R.raw.sneeze), null);
// 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(notificationChannel);
}
并显示通知:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Notification notification = new Notification.Builder(this, CHANNEL_TEST)
.setSmallIcon(R.drawable.logo)
.setContentTitle(title)
.setContentText(message)
.setPriority(Notification.PRIORITY_HIGH)
.build();
/// Show the notification
notificationManager.notify(0, notification);
}
更新
我的 android 版本低于 android O
/// Create a Notification Builder
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_TEST)
.setSmallIcon(R.drawable.logo) /// Set notification's icon
.setSound(Uri.parse("android.resource://" + getPackageName() + '/' + R.raw.tuturu)) /// Set sound
.setAutoCancel(true) /// Automatically remove notification when user taps it
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000}) /// Set sound vibration
.setOnlyAlertOnce(true) /// Only alert Once
.setContentIntent(pendingIntent) /// Set intent it will show when click notification
.setContent(getCustomDesign(title, message)); /// Set the design of notification
notificationManager.notify(0, builder.build());
我用androidPie 9.0来测试
我猜这可能是因为您无法在创建 NotificationChannel 后对其进行编辑。我认为一旦创建它将保留原始配置(第一次创建时使用的配置)。要使其正常工作,您需要删除应用程序(卸载它)并使用新设置重新安装它,我的意思是新的声音文件。
我不知道为什么,但无论我如何更改原始文件中的 mp3 声音,通知只会发出一种声音,它的名称是最少的字母,例如:My raw file,通知将播放message_tone 无论我在代码中更改什么名称:
/// Set sound for channel
notificationChannel.setSound(Uri.parse("android.resource://" + getPackageName() + '/' + R.raw.sneeze), null);
我的频道设置代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
/// Set channel's ID, name, and importance
NotificationChannel notificationChannel = new NotificationChannel(
CHANNEL_TEST,
"OUTFITTERX",
NotificationManager.IMPORTANCE_HIGH
);
/// Set sound for channel
notificationChannel.setSound(Uri.parse("android.resource://" + getPackageName() + '/' + R.raw.sneeze), null);
// 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(notificationChannel);
}
并显示通知:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Notification notification = new Notification.Builder(this, CHANNEL_TEST)
.setSmallIcon(R.drawable.logo)
.setContentTitle(title)
.setContentText(message)
.setPriority(Notification.PRIORITY_HIGH)
.build();
/// Show the notification
notificationManager.notify(0, notification);
}
更新
我的 android 版本低于 android O
/// Create a Notification Builder
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_TEST)
.setSmallIcon(R.drawable.logo) /// Set notification's icon
.setSound(Uri.parse("android.resource://" + getPackageName() + '/' + R.raw.tuturu)) /// Set sound
.setAutoCancel(true) /// Automatically remove notification when user taps it
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000}) /// Set sound vibration
.setOnlyAlertOnce(true) /// Only alert Once
.setContentIntent(pendingIntent) /// Set intent it will show when click notification
.setContent(getCustomDesign(title, message)); /// Set the design of notification
notificationManager.notify(0, builder.build());
我用androidPie 9.0来测试
我猜这可能是因为您无法在创建 NotificationChannel 后对其进行编辑。我认为一旦创建它将保留原始配置(第一次创建时使用的配置)。要使其正常工作,您需要删除应用程序(卸载它)并使用新设置重新安装它,我的意思是新的声音文件。