Harry Nguyễn
Harry Nguyễn

Reputation: 11

Android notification channel sound doesn't work

I don't know why but no matter what I change the mp3 sound in raw file, the notification just make one sound that its name is the least alphabet, ex: My raw file, the notification will play message_tone no matter what I change the name in the code:

/// Set sound for channel
            notificationChannel.setSound(Uri.parse("android.resource://" + getPackageName() + '/' + R.raw.sneeze), null);

My code for set up channel:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            /// Set channel's ID, name, and importance
            NotificationChannel notificationChannel = new NotificationChannel(
                    CHANNEL_TEST,
                    "OUTFITTERX",
                    NotificationManager.IMPORTANCE_HIGH
            );

            /// Set sound for channel
            notificationChannel.setSound(Uri.parse("android.resource://" + getPackageName() + '/' + R.raw.sneeze), null);

            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(notificationChannel);
        }

And show notification:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Notification notification = new Notification.Builder(this, CHANNEL_TEST)
                    .setSmallIcon(R.drawable.logo)
                    .setContentTitle(title)
                    .setContentText(message)
                    .setPriority(Notification.PRIORITY_HIGH)
                    .build();

            /// Show the notification
            notificationManager.notify(0, notification);
}

UPDATE

My code for android version lower than android O

/// Create a Notification Builder
            NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_TEST)
                    .setSmallIcon(R.drawable.logo) /// Set notification's icon
                    .setSound(Uri.parse("android.resource://" + getPackageName() + '/' + R.raw.tuturu)) /// Set sound
                    .setAutoCancel(true) /// Automatically remove notification when user taps it
                    .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000}) /// Set sound vibration
                    .setOnlyAlertOnce(true) /// Only alert Once
                    .setContentIntent(pendingIntent) /// Set intent it will show when click notification
                    .setContent(getCustomDesign(title, message)); /// Set the design of notification

            notificationManager.notify(0, builder.build());

And I use android Pie 9.0 to test

Upvotes: 0

Views: 882

Answers (1)

sebasira
sebasira

Reputation: 1834

I'm guessing that this could be because you can not edit a NotificationChannel once created. I think once created it will keep the original configuration (the one used when first creating it). To make it work you will need to delete the app (unistall it) and re install it with the new settings, I mean the new sound file.

Upvotes: 1

Related Questions