1

I'm working with notification channels and as standard I want to enable sound for notification. I set sound in NotificationChannel options, but it's disabled. I also tried to set Audio in the NotificationCompat.Builder but this either didn't worked.

Ps: I don't know if my phone is the problem, I have an Redmi Note 7 with Android 9 and MIUI.

I've tried in the Channel options, NotificationCompat.Builder, both and each once solo, but it didn't worked. But if I turn the option in the Channel settings on it works but I don't want that the user needs to do that.

    String CHANNEL_ID = "coin_master_item_generator";
    CharSequence CHANNEL_NAME = "Item Generator Coin Master";

    AudioAttributes audioAttributes = new AudioAttributes.Builder()
    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
    .build();


    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
    channel.setDescription("Notifies when Items are generated");
    channel.enableVibration(true);
    channel.setVibrationPattern(new long[]{2000,1000});
    channel.enableLights(true);
    channel.setLightColor(Color.WHITE);
    channel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION),audioAttributes);

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.createNotificationChannel(channel);


    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_launcher_foreground)
        .setContentTitle("Item Generator")
        .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
        .setContentText("New Items generated");

    notificationManager.notify(1524,builder.build());
J.Doe
  • 177
  • 1
  • 2
  • 13
  • Try it on an emulator also, then you will know if its your phone or not – Blundell Jul 07 '19 at 20:34
  • Also have you checked what `Uri` this line of code returns? `RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)` it may be null until like you said, the user enables it. You could write a bit of code to use that Uri and play the sound - to see if thats the problem. – Blundell Jul 07 '19 at 20:38

0 Answers0