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
2
Answers
creating new channel id and replacing the old channel id seems do the trick for this. im still dont know why tho, if anyone know please explain i'd be please to why is this happening. Thanks.
Notifications: Types and Use:
Why Updating Sound Requires a New Channel:
New Channel ID :
Delete Old Channel :
Create New Notification Channel:
Advantages Accrued by Using the Same ID:
Notes:
This way, android app developer can handle the notification channels, the channel ID, and the notification sounds to ensure that their users are always provided with an effective experience in spite of changes in the sound notification for the app.
More stuffs to read :
https://developer.android.com/develop/ui/views/notifications/channels
https://medium.com/android-news/android-notifications-an-elegant-way-to-build-and-display-7771d65ba3a2