如何更改通知声音Android Java



我试图创建一个具有3种不同通知声音的应用程序(但不是不同的通知通道),我创建自定义通知声音,但当我尝试改变声音时,它仍然保持不变,我还试图删除通道并再次创建它,但通知声音不改变。

首先创建通道:

String CHANNEL_ID = Constants.notification_channel_ID;
int NOTIFICATION_ID = 1991;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager mNotificationManager = getSystemService(NotificationManager.class);
NotificationChannel existingChannel = mNotificationManager.getNotificationChannel(CHANNEL_ID);
if (existingChannel != null){
mNotificationManager.deleteNotificationChannel(CHANNEL_ID);
}
CharSequence name = getResources().getString(R.string.drink_reminder);
String description = getResources().getString(R.string.notification_to_remind_you_to_drink);
int importance = NotificationManager.IMPORTANCE_HIGH;
if (s1 = true){
Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.test_sound);
}else{
Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.test_sound1);
}
AudioAttributes audioAttributes = new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION ).setUsage(AudioAttributes.USAGE_NOTIFICATION).build() ;
NotificationChannel notificationChannel = new NotificationChannel( CHANNEL_ID , name , importance) ;
notificationChannel.setDescription(description);
notificationChannel.enableLights(true) ;
notificationChannel.setLightColor(Color.BLUE) ;
notificationChannel.enableVibration( tinyDB.getBoolean(Constants.settings_notification_vibration_key,true));
notificationChannel.setVibrationPattern( new long []{ 100 , 200 , 300 , 400 , 500 , 400 , 300 , 200 , 400 }) ;
notificationChannel.setSound(sound , audioAttributes) ;
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(notificationChannel);
}

然后创建notification:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(this.getResources().getString(R.string.app_name))
.setContentText(this.getResources().getString(R.string.text))
.setContentIntent(pendingIntent)
.setSound(sound)
.setChannelId(CHANNEL_ID)
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID, builder.build());

所以在第一次这个代码工作时,当后来我再次调用这个方法来改变声音时,通知仍然在开始时使用相同的声音,我也不能禁用,启用振动(它保持相同,就像第一次配置一样)。如你所见,我也在尝试重新创建通知通道,但还是一样的。如有任何帮助,不胜感激。

通过设置静音通知并添加手动声音和振动来解决。

当我创建通道时,我使用:
notificationChannel.enableVibration(false);
notificationChannel.setSound(null,null);

另外,当我构建通知时,我添加了以下代码:

.setSilent(true)

震动,我用这个:

private void vibrate (Context context){
Vibrator v = (Vibrator) context.getSystemService(VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
v.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
//deprecated in API 26
v.vibrate(500);
}
}

和通知铃声我使用这个代码:

private void playSound(Context context, String SoundUri){
Uri rawPathUri = Uri.parse(SoundUri);
Ringtone r = RingtoneManager.getRingtone(context, rawPathUri);
r.play();
}

当SoundUri是与用户所选择的设置相匹配的字符串时

最新更新