Android — Change Notification Sound

--

Set or change notification sound for notification channel

As mentioned in the official documentation

Starting in Android 8.0 (API level 26), all notifications must be assigned to a channel. For each channel, you can set the visual and auditory behavior that is applied to all notifications in that channel. Then, users can change these settings and decide which notification channels from your app should be intrusive or visible at all.

You can find more in the docs

The code block for creating a notification channel:

private fun createNotificationChannel(notificationChannelId: String?, soundUri: Uri?) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name: CharSequence = "Testing notification $notificationChannelId"
val description = "Added to testing of notification sound"
val importance = NotificationManager.IMPORTANCE_HIGH
val channel =
NotificationChannel("text_notification_id$notificationChannelId", name, importance)
channel.description = description
// setting notification sound here
channel.setSound(
soundUri,
Notification.AUDIO_ATTRIBUTES_DEFAULT
)
NotificationManagerCompat.from(this)
.createNotificationChannel(channel)
}
}

Once we have created a notification channel then we can change only a few things that we can in the notification channel like title and description. I doesn’t allow us to change in notification sound.

After you create a notification channel, you cannot change the notification behaviors — the user has complete control at that point. Though you can still change a channel’s name and description.

So what if we want to change the notification sound after the creation of the notification channel? 🤔

The first thing that comes to your mind is — Let’s recreate the notification channel by deleting the channel and creating again. But but but it doesn’t work if we create channels again with the same id because a fun fact is here i.e

If you create a new channel with this same id, the deleted channel will be un-deleted with all of the same settings it had before it was deleted.

You can find more in the docs

if we create a channel with ID “A”, delete it, and create a new channel with ID “A” — the framework will restore the previously deleted channel with this ID.

So we need to create a channel with a different id. Here in the below code, our identifier is a sound name that we are saving in shared-preference so that we can use it later to delete the old channel.

The code block for re-creating a notification channel

private var sharedPref: SharedPreferences? = null
private var sharedPrefEditor: SharedPreferences.Editor? = null
private fun recreateChannel(soundUri: Uri?) {sharedPref = getSharedPreferences("shared_pref", Activity.MODE_PRIVATE)
sharedPrefEditor = sharedPref?.edit()

val previousId: String = sharedPref?.getString("notification_id", "") ?: "raw"
val newChannelId: String = soundUri?.getQueryParameter("title") ?: "raw"
sharedPrefEditor?.putString("notification_id", newChannelId)
sharedPrefEditor?.apply()

deleteNotificationChannel(previousId)
createNotificationChannel(newChannelId, soundUri)
}
private fun deleteNotificationChannel(channelId: String?) {
NotificationManagerCompat.from(this).deleteNotificationChannel(
"text_notification_id$channelId"
)
}

Now you are thinking about from where we get soundUri? 🤦🏻‍♂️

Don’t worry let’s follow the below code to choose the notification sound.

private var chooseRingtoneLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val uri =
result.data?.getParcelableExtra<Uri>(RingtoneManager.EXTRA_RINGTONE_PICKED_URI)
uri?.let {
recreateChannel(it)
}
}
}
// Call this method from where you want to allow user to select notification sound.
private fun selectNotificationSound() {

val intent = Intent(RingtoneManager.ACTION_RINGTONE_PICKER)
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION)
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone")
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, null as Uri?)
chooseRingtoneLauncher.launch(intent)

}

As per my knowledge, I have covered everything here. If I missed out on anything here or have any doubt regarding notification then please add a comment here. Like and follow for more if it works for you.

--

--

Rahul Rathore (Flutter and Android Developer)
Rahul Rathore (Flutter and Android Developer)

No responses yet