Skip to content
Advertisement

Сan’t set custom sound and vibration for notifications on Android Oreo+

I can’t set custom notification sound and vibration pattern for android oreo and above. For earlier versions of android everything works as it should. I wrote my code based on these answers: Custom notification sound , android Oreo?,
Custom Notification Sound not working in Android Oreo and others. But none of this had the desired result. The notification comes, but without sound and vibration. The values of isSoundEnabled and isVibrationEnabled are transmitted correctly.

My method:

   private void sendNotif(String title, String text, boolean isSoundEnabled, boolean isVibrationEnabled) {

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);

        String channelName = "EEE";
        String channelId = "CHANNEL_1";
        long[] vibrationPattern = new long[]{40, 75, 75, 75, 75, 75, 75, 750};
        long[] vibrationEmpty = new long[]{0};
        Uri soundUri = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.notification);

        Intent intent = new Intent(context, ActivityOne.class);
        PendingIntent pIntent = PendingIntent
                .getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        NotificationCompat.Builder builder;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            builder = new NotificationCompat.Builder(context, channelId)
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle(title)
                            .setContentText(text)
                            .setPriority(NotificationCompat.PRIORITY_MAX)
                            .setAutoCancel(true)
                            .setContentIntent(pIntent);

            NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);

            channel.enableLights(true);

            if (isSoundEnabled) {
                AudioAttributes audioAttributes = new AudioAttributes
                        .Builder()
                        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                        .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                        .build();
                channel.setSound(soundUri, audioAttributes);
            } else {
                channel.setSound(null, null);
            }

            channel.enableVibration(isVibrationEnabled);

            if (isVibrationEnabled){
                channel.setVibrationPattern(vibrationPattern);
            } else {
                channel.setVibrationPattern(vibrationEmpty);
            }
            notificationManager.createNotificationChannel(channel);
            builder.setChannelId(channelId);


        } else {

            builder = new NotificationCompat.Builder(context, channelName)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(title)
                    .setContentText(text)
                    .setPriority(NotificationCompat.PRIORITY_MAX)
                    .setAutoCancel(true)
                    .setContentIntent(pIntent);

            if (isSoundEnabled) {
                builder.setSound(soundUri);
            }
            if (isVibrationEnabled) {
                builder.setVibrate(vibrationPattern);
            } else {
                builder.setVibrate(vibrationEmpty);
            }
        }

        notificationManager.notify(new Random().nextInt(10000), builder.build());
    }

What am I doing wrong?

Advertisement

Answer

Figured it out. The checks if (isSoundEnabled) and if (isVibrationEnabled) were unnecessary. To control vibration and notification sounds on android oreo+, you need to create separate channels.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement