Java 通过setSound()设置自定义通知声音(和振动)无效
尝试在我的应用程序中创建通知时,设置自定义通知声音无效,将使用系统默认声音。另外,我正在尝试在我的通知上启用振动,这也不起作用 我试图将声音设置如下(相关片段,请参阅下面的完整代码): 我的自定义声音是一个1秒长的声音片段,以44.1 kHz的频率采样,存储为Java 通过setSound()设置自定义通知声音(和振动)无效,java,android,audio,notifications,vibration,Java,Android,Audio,Notifications,Vibration,尝试在我的应用程序中创建通知时,设置自定义通知声音无效,将使用系统默认声音。另外,我正在尝试在我的通知上启用振动,这也不起作用 我试图将声音设置如下(相关片段,请参阅下面的完整代码): 我的自定义声音是一个1秒长的声音片段,以44.1 kHz的频率采样,存储为res/raw/bell.wav。我还尝试将代码片段转换为其他不同的格式,我尝试了mp3和ogg,但没有成功。 关于振动:android.permission.vibration包含在清单中 我不知道我做错了什么。下面是创建和发布通知的se
res/raw/bell.wav
。我还尝试将代码片段转换为其他不同的格式,我尝试了mp3
和ogg
,但没有成功。
关于振动:android.permission.vibration
包含在清单中
我不知道我做错了什么。下面是创建和发布通知的sendNotification()
函数的完整代码
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.bell);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_launcher_web)
.setContentTitle("New order")
.setContentText(messageBody)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Order notifications",
NotificationManager.IMPORTANCE_MAX);
notificationManager.createNotificationChannel(channel);
AudioAttributes att = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build();
channel.setSound(sound, att);
channel.setVibrationPattern(new long[]{0, 500});
} else {
notificationBuilder
.setVibrate(new long[]{0, 500})
.setSound(sound);
}
notificationManager.notify(0, notificationBuilder.build());
}
创建通知频道后,您无法更改灯光、声音和振动等内容。也许这就是问题所在。要查看更改,您需要先删除应用程序,然后重新安装。感谢您的回答,不幸的是,即使在卸载应用程序,甚至更改频道名称之后,这也无法解决我的问题
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.bell);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_launcher_web)
.setContentTitle("New order")
.setContentText(messageBody)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Order notifications",
NotificationManager.IMPORTANCE_MAX);
notificationManager.createNotificationChannel(channel);
AudioAttributes att = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build();
channel.setSound(sound, att);
channel.setVibrationPattern(new long[]{0, 500});
} else {
notificationBuilder
.setVibrate(new long[]{0, 500})
.setSound(sound);
}
notificationManager.notify(0, notificationBuilder.build());
}