Я пытаюсь создать в Android канал уведомлений, который будет воспроизводить звук из моей папки Raw.
Как видно из приведенного ниже кода, я поставил эту строку:
RingtoneManager.getRingtone (a, uri) .play ();
Это позволяет мне проверить правильность Uri, и это действительно так.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = a.getString(R.string.channel_name);
NotificationManager notificationManager = a.getSystemService(NotificationManager.class);
notificationManager.deleteNotificationChannel(a.getString(R.string.default_notification_channel_id));
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(a.getApplicationContext());
Uri uri;
uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + a.getPackageName() + "/raw/" + prefs.getString("pref_ring", "a1"));
RingtoneManager.getRingtone(a, uri).play();
//CORRECT SOUND IS PLAYING, URI IS OK
String description = a.getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(a.getString(R.string.default_notification_channel_id), name, importance);
channel.setDescription(description);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
channel.setSound(uri, audioAttributes);
notificationManager.createNotificationChannel(channel);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(a, a.getString(R.string.default_notification_channel_id))
.setSmallIcon(R.drawable.logo)
.setContentTitle("TEST")
.setContentText("TEST")
.setAutoCancel(true)
.setSound(uri)//Just in case, but has no effect
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
notificationManager.notify(0, mBuilder.build());
}
К сожалению, уведомление о том, чтовсегда играет мелодию уведомления по умолчанию
Есть идеи, что я делаю не так?