https://kotlinlang.org logo
#android
Title
# android
a

Akash Singh

05/31/2023, 2:12 PM
Hi, I am trying to set a mp3 file as the notification sound for my app, I have followed all the steps mentioned in the documentation as well as some StackOverflow posts but still I always get the default notification sound on receiving the notification. Can anyone please help. Attaching code snippets for reference:
Copy code
val sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.packageName + "/raw/test_sms")
val notificationBuilder = NotificationCompat.Builder(context, channelId)
     .setColor(CommonUtil.getColorFromAttr(R.attr.orange))
     .setSmallIcon(R.drawable.notification_icon_transparent)
     .setAutoCancel(true)
     .setDeleteIntent(getDeleteIntent(context, data, payload))
     .setContentIntent(getContentIntent(context, data))
     .setSound(sound)
     .setPriority(data.notificationPriority)

val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val build = notificationBuilder.build()
build.flags.and(Notification.FLAG_AUTO_CANCEL)
data.notificationId?.let { notificationManager.notify(it, build)
For Notification Channel creation (Android O and above)
Copy code
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notificationChannel = NotificationChannel(data.channelId, data.channelName,
    data.channelPriority ?: NotificationManager.IMPORTANCE_HIGH
)
notificationChannel.enableLights(true)
notificationChannel.lightColor = Color.RED
notificationChannel.enableVibration(true)

val sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.packageName + "/raw/test_sms")
val audioAttributes = AudioAttributes.Builder()
    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
    .setUsage(AudioAttributes.USAGE_ALARM)
    .build()

notificationChannel.setSound(sound, audioAttributes)

data.channelDesc?.let {
    notificationChannel.description = it
}

notificationManager.createNotificationChannel(notificationChannel)
not kotlin but kotlin colored 6
2 Views