gpt4 book ai didi

java - Android 通知 channel 声音不起作用

转载 作者:行者123 更新时间:2023-12-01 17:28:14 28 4
gpt4 key购买 nike

我不知道为什么,但无论我如何更改原始文件中的 mp3 声音,通知都会发出一种声音,表明其名称是最小的字母,例如:My raw file ,无论我在代码中更改名称,通知都会播放 message_tone:

/// Set sound for channel
notificationChannel.setSound(Uri.parse("android.resource://" + getPackageName() + '/' + R.raw.sneeze), null);

我的设置 channel 代码:

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());

我使用android Pie 9.0来测试

最佳答案

我猜测这可能是因为您无法编辑创建后的NotificationChannel。我认为一旦创建它将保留原始配置(第一次创建时使用的配置)。要使其正常工作,您需要删除该应用程序(卸载它)并使用新设置重新安装它,我指的是新的声音文件。

关于java - Android 通知 channel 声音不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61176044/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com