Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 8.1 notifications showing but not making any sounds

I have a service which runs in foreground with, among other things, a stopwatch. When the service starts the notification appears, and when it ends I update it with sound and some text that indicates that it's finished. The problem is that this works well until api 25, but for 26 and 27 it updates the notification alright, but makes no sound. Here's the relevant code:

The creation of the notification inside the service:

mBuilder = new NotificationCompat.Builder(context, "Main")
            .setSmallIcon(R.drawable.ic_notification_temp)
            .setContentTitle(step.getDescription())
            .setContentText(getString(R.string.notification_text))
            .setVisibility(VISIBILITY_PUBLIC)
            .setOngoing(true);
            .setDeleteIntent(deletePendingIntent);
            .setContentIntent(contentPendingIntent);
            .addAction(R.drawable.ic_stop_red_24dp, getString(R.string.cancel),finnishPendingIntent);

mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(mId, mBuilder.build());

The update to the notification builder when the "work" is finished:

mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));        
mBuilder.setVibrate(new long[]{0, 300, 0, 400, 0, 500});
mBuilder.setOngoing(false);
mBuilder.setAutoCancel(true);
mBuilder.setContentText(getString(R.string.notification_finished_text));

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    mBuilder.setChannelId("Sound");
    mBuilder.setCategory(NotificationCompat.CATEGORY_ALARM);
}
mNotificationManager.notify(mId, mBuilder.build());

The 2 channels created just for api 26 or up on app start:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    // Create the normal NotificationChannel
    CharSequence name = getString(R.string.app_name);
    int importance = NotificationManager.IMPORTANCE_LOW;
    NotificationChannel channel = new NotificationChannel("Main", name, importance);
    // Register the channel with the system; you can't change the importance
    // or other notification behaviors after this
    NotificationManager notificationManager = (NotificationManager) getSystemService(
            NOTIFICATION_SERVICE);
    notificationManager.createNotificationChannel(channel);

    // Create theNotificationChannel with sound
    importance = NotificationManager.IMPORTANCE_HIGH;
    name = getString(R.string.notification_channel_sound);
    NotificationChannel sound = new NotificationChannel("Sound", name, importance);
    sound.enableVibration(true);
    sound.setVibrationPattern(new long[]{0, 300, 0, 400, 0, 500});
    AudioAttributes aa = new AudioAttributes.Builder()
            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
            .setLegacyStreamType(AudioManager.STREAM_NOTIFICATION)
            .setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT)
            .build();
    sound.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), aa);
    notificationManager.createNotificationChannel(sound);
}

And that's where I'm at now. I've tried to not use setsound() as I've read that the notification should just play the default sound with the appropriate importance (and of course I've even uninstalled the app between tries to correctly update the channel settings) but nothing seems to work for api 26 and I just don't know what I'm doing wrong.

like image 570
TXinTXe Avatar asked Oct 16 '22 21:10

TXinTXe


1 Answers

I had the exact same problems with most of my emulator AVDs. The following fixed it for me:

  • start the affected AVD
  • long press the AVD's power button
  • restart the AVD

Afterwards it should work again.

like image 54
mdiener Avatar answered Oct 20 '22 20:10

mdiener