Skip to content Skip to sidebar Skip to footer

Android 8 Notifications Setsound Not Working

I have the following code but everytime I just hear the default android sound. // create channel NotificationChannel channel = new NotificationChannel(ANDROID_CHA

Solution 1:

I tried to see the difference between your sound file and mine. I used Audacity software. Your sound file has sampling rate 22050Hz while the sound files i use are sampled at 44100Hz. So i converted your sound file sampling rate to 44100Hz and used that as notification sound. Now it works.

The problem is with the sound file. May be it's new change in Android O because it's working fine on older Android version.

This is how to resample-enter image description here

Solution 2:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            Notification.BuildernotificationBuilder=newNotification.Builder(MyApplication.getInstance().getApplicationContext(), NOTIFICATION_CHANNEL_ID)
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle(pTitle)
                            .setContentText(messageBody)
                            .setAutoCancel(true)
                            //.setPriority(Notification.PRIORITY_MAX) // this is deprecated in API 26 but you can still use for below 26. check below update for 26 API//.setSound(defaultSoundUri)
                            .setContentIntent(pendingIntent);

            NotificationChannelnotificationChannel=newNotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);
            // Configure the notification channel.AudioAttributesatt=newAudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                    .build();
            notificationChannel.setSound(defaultSoundUri,att);
            notificationChannel.setDescription(messageBody);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setVibrationPattern(newlong[]{0, 1000, 500, 1000});
            notificationChannel.enableVibration(true);
            notificationManager.createNotificationChannel(notificationChannel);
            if (imageThumbnail != null) {
                notificationBuilder.setStyle(newNotification.BigPictureStyle()
                        .bigPicture(imageThumbnail).setSummaryText(messageBody));
            }
            notificationManager.notify(0/* ID of notification */, notificationBuilder.build());

        } else {
            NotificationCompat.BuildernotificationBuilder=newNotificationCompat.Builder(MyApplication.getInstance().getApplicationContext())
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle(pTitle)
                            .setContentText(messageBody)
                            .setAutoCancel(true)
                            .setPriority(Notification.PRIORITY_MAX) // this is deprecated in API 26 but you can still use for below 26. check below update for 26 API
                            .setSound(defaultSoundUri)
                            .setContentIntent(pendingIntent);
            if (imageThumbnail != null) {
                notificationBuilder.setStyle(newNotificationCompat.BigPictureStyle()
                        .bigPicture(imageThumbnail).setSummaryText(messageBody));
            }
            notificationManager.notify(0/* ID of notification */, notificationBuilder.build());

        }

Post a Comment for "Android 8 Notifications Setsound Not Working"