The notification sound on my flutter android app is not updated by installing new version of the app and only will update after I uninstall and reinstall it.
This is my MainActivity.kt:
import android.app.NotificationChannel
import android.app.NotificationManager
import android.media.AudioAttributes
import android.net.Uri
import android.os.Build
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant
import android.util.Log
class MainActivity: FlutterActivity() {
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
GeneratedPluginRegistrant.registerWith(flutterEngine)
createNotificationChannel()
}
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channelId = "channel_id"
val channelName = "Channel Name"
val importance = NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel(channelId, channelName, importance)
val soundUri = Uri.parse("android.resource://" + packageName + "/" + R.raw.alert)
val audioAttributes = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build()
channel.setSound(soundUri, audioAttributes)
val notificationManager = getSystemService(NotificationManager::class.java)
notificationManager?.deleteNotificationChannel(channelId)
notificationManager.createNotificationChannel(channel)
}
}
}
I thought it was because the existing channel id so I tried to delete it first by using:
notificationManager?.deleteNotificationChannel(channelId)
I was expecting the notification sound to be updated once I install the new version of the app and not having to uninstall it first.
Why updating a notification sound in Android requires creating a new notification channel ID.
Notifications: Types and Use:
Why Updating Sound Requires a New Channel:
New Channel ID :
Delete Old Channel :
fun deleteOldChannel(context: Context, oldChannelId: String) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager = context.getSystemService(NotificationManager::class.java)
notificationManager?.deleteNotificationChannel(oldChannelId)
}
}
Create New Notification Channel:
fun createNotificationChannel(context: Context, channelId: String, channelName: String, channelDescription: String) {
if (Build.VERSION.SDK_INT >= Build.VERSION_ CODES.O) {
val notificationManager = context.getSystemService(NotificationManager::class.java)
val importance = NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel(channelId, channelName, importance)
channel.description = channelDescription
// Configure the sound
val soundUri = getSoundUri(context) // Get sound URI from helper function (see below)
val audioAttributes = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build()
channel.setSound(soundUri, audioAttributes)
notificationManager?.createNotificationChannel(channel)
}
}
// Helper function to get sound URI (replace with your sound file)
private fun getSoundUri(context: Context): Uri {
return RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
// OR: You can use a custom sound from raw resources
// return Uri.parse("android.resource://" + context.packageName + "/" + R.raw.your_sound_file)
}
Advantages Accrued by Using the Same ID:
Notes:
More stuffs to read :
https://developer.android.com/develop/ui/views/notifications/channels