У меня есть это приложение, в котором я хочу установить собственный звук уведомления,
у меня есть этот метод, который проверяет версию Android и создает соответствующее уведомление..
на Android 6 и более ранних версиях он работает нормально и воспроизводит собственный звук.
но на Android 7 и выше не воспроизводится настраиваемый звук уведомления, который я хочу
1- ни один из ответов о переполнении стека не смог решить эту проблему
2- мой код написан наилучшим образом, как указано в документах разработчиков Android
3- мой файл .wav и его 44100 Гц
вот код, что мне не хватает?
private void createLocalNotificationx(Intent intent) {
NotificationManager notificationManager;
PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
Uri alarmSound;
if (type.equals(VALUE_ROUTINE)) {
alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + getPackageName() + "/" + R.raw.alarm);
} else {
alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + getPackageName() + "/" + R.raw.emergency);
}
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification mNotification = new Notification.Builder(this)
.setOngoing(false)
.setContentTitle(title)
.setContentText(body)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setSound(alarmSound).setPriority(Notification.PRIORITY_DEFAULT)
.build();
notificationManager.notify(0, mNotification);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Log.d(TAG, "alarmSound =" + alarmSound.getPath());
/*First create the notification channel:*/
String CHANNEL_ID = getPackageName() + "/ID";
String name = getString(R.string.app_name);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 500});
mChannel.enableLights(true);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
notificationManager.createNotificationChannel(mChannel);
Notification mNotification = new Notification.Builder(this, CHANNEL_ID)
.setDefaults(Notification.DEFAULT_ALL | Notification.DEFAULT_LIGHTS)
.setOngoing(false)
.setContentTitle(title)
.setContentText(body)
.setSound(alarmSound)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendingIntent)
.build();
notificationManager.notify(0, mNotification);
}
}
примечание: это может быть более чистый код, но он должен быть в порядке!
mChannel.setSound(тревога) и mNotification.setChannelId(CHANNEL_ID)