Android 10: No custom sound in notification

Viewed 497

I'm creating a channel with custom sound. The notifications are correctly displayed but the sound played is still the default one.

Creation of the channel


val audioAttribute = AudioAttributes.Builder()
  .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
  .setUsage(AudioAttributes.USAGE_NOTIFICATION)
  .build()


val sound = Uri.Builder().scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
  .authority(packageName)
  .path(R.raw.notifsound.toString()).build()


alertChannel.apply {
    enableVibration(true)
    description = "Signal alert channel"
    lockscreenVisibility = Notification.VISIBILITY_PUBLIC
    setSound(sound, audioAttribute)
}

notificationManager.createNotificationChannels(listOf(locationNotificationChannel,gpsStatusChannel,alertChannel))

I checked the URI with this

(contentResolver as ContentResolver).openInputStream(sound)

And it can read the file.

2 Answers

I found the problem.

Once you create a notification channel, you can't change the sound of it.

So you have two solutions:

  • Uninstall the app and install again
  • Or change the notification channel id to create a new one with the right configuration.

Hope this help.

I solved the issue by uninstalling the app and reinstalling it

 private fun sendNotification(remoteMessage: RemoteMessage) {

    val fullScreenIntent = Intent(this, IncomingActivity::class.java).apply {
        flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
        putExtra(Constants.ROOM_ID, remoteMessage.data[Constants.ROOM_ID])
        putExtra(Constants.REMOTE_MSG_INVITER_TOKEN, remoteMessage.data[Constants.REMOTE_MSG_INVITER_TOKEN])
    }
    val fullScreenPendingIntent: PendingIntent? = TaskStackBuilder.create(this).run {
        // Add the intent, which inflates the back stack
        addNextIntentWithParentStack(fullScreenIntent)
        // Get the PendingIntent containing the entire back stack
        getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
    }
    val audioAttributes = AudioAttributes.Builder()
            .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
            .build()
    val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val notificationChannel = NotificationChannel(resources.getString(R.string.app_name), "Incoming call...", NotificationManager.IMPORTANCE_HIGH)
        notificationChannel.description = "Someone is calling you..."
        notificationChannel.setSound(Settings.System.DEFAULT_RINGTONE_URI, audioAttributes)
        notificationManager.createNotificationChannel(notificationChannel)
    }

    val notificationBuilder =
            NotificationCompat.Builder(this, resources.getString(R.string.app_name))
                    .setSmallIcon(R.drawable.icon)
                    .setContentTitle("Incoming call")
                    .setContentText("Someone is calling you...")
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setCategory(NotificationCompat.CATEGORY_CALL)
                    .setAutoCancel(true)
                    .setSound(Settings.System.DEFAULT_RINGTONE_URI)
                    .setChannelId(resources.getString(R.string.app_name))
                    .setContentIntent(fullScreenPendingIntent)
                    .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.icon))

    val incomingCallNotification = notificationBuilder.build()
    notificationManager.notify(0, incomingCallNotification)

}
Related