1

I have set custom sound for notification. It's working for some device but not working on some device mostly Xiaomi device.

I have used this code:

private fun createNotificationChannel(){
    val context = AppApplication.getContext()
    val notificationManagerCompat = NotificationManagerCompat.from(context)
    preChannelIds.forEach {
        deleteChannel(notificationManagerCompat,it)
    }
    if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        val attributes = getAudioAttributes()
        val soundUri = getSoundUri(context)
        val importance = NotificationManagerCompat.IMPORTANCE_HIGH
        val channelBuilder = NotificationChannelCompat.Builder(channelId, importance).apply {
            setName(channelName)
            setDescription(channelDescription)
            setSound(soundUri, attributes)
            setVibrationPattern(vibrationPattern)
            setLightsEnabled(true)
            setShowBadge(true)
        }
        val channel = channelBuilder.build()
        notificationManagerCompat.createNotificationChannel(channel)
    }
}



fun sendNotification(title:String, message:String){

    val context = AppApplication.getContext()
    val intent = ....

    val notificationManagerCompat = NotificationManagerCompat.from(AppApplication.getContext())

    val pendingIntent = PendingIntent.getActivity(
        AppApplication.getContext(), notificationRequestCode, intent,
            PendingIntent.FLAG_UPDATE_CURRENT)

    val notificationBuilder = NotificationCompat.Builder(AppApplication.getContext(), channelId)
        .setSmallIcon(getNotificationIcon())
        .setContentTitle(title)
        .setContentText(message)
        .setStyle(NotificationCompat.BigTextStyle().bigText(message))
        .setAutoCancel(true)
        .setVibrate(vibrationPattern)
        .setColor(ContextCompat.getColor(AppApplication.getContext(), R.color.colorPrimary))
        .setContentIntent(pendingIntent)

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O){
        notificationBuilder.priority = NotificationCompat.PRIORITY_MAX
        notificationBuilder.setSound(getSoundUri(context))
    }

    val notification = notificationBuilder.build()
    notification.flags = Notification.FLAG_INSISTENT

    notificationManagerCompat.notify(notificationRequestCode, notification)
}

Please help me to solve this issue for some device. Is there any other solution for this issue.

Abu Yousuf
  • 5,729
  • 3
  • 31
  • 50
  • 1
    manufacturers messed up notification system,. best you can wish is actually user receive notifications – ProblemSlover Oct 12 '21 at 14:58
  • lol @ProblemSlover – Joe Oct 12 '21 at 15:05
  • A chance your sound isnt getting downloaded in time? or its unavailable at the time of the notification – Joe Oct 12 '21 at 15:06
  • @Joe i used local file for sound – Abu Yousuf Oct 12 '21 at 15:07
  • Well for one thing, if the phone isnt < VERSION_CODES.O then the sound wont play – Joe Oct 12 '21 at 15:09
  • I also heard that some device didn't get notification sound for well known apps too – Abu Yousuf Oct 12 '21 at 15:11
  • @Joe why sound wont play i have set sound in channel if device > VERSION_CODES.O – Abu Yousuf Oct 12 '21 at 15:12
  • you have if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O){ playsound() } So if their build version is less then Build.VERSION_CODES.O then the set sound method will execute. You might want to change it to if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ – Joe Oct 12 '21 at 15:14
  • if device >= Build.VERSION_CODES.O i am setting sound in notification channel. I also checked some device with Android version 8 also didn't play sound. It only play when user set different sound from setting notification channel – Abu Yousuf Oct 12 '21 at 15:19

0 Answers0