In MI Note 5 Pro which has latest MI UI 10.0 with Oreo, so when I try to send push notification by default sound is disable, so i am not able to enable sound programmatically when I am creating a channel for that.
In other Oreo devices notification sound is coming but in MI custom Oreo OS Sound is by default disable
Let me show my code for notification :
var intent = Intent(mContext, HomeActivity::class.java)
intent.putExtra(Constants.EXTRA_FROM_NOTIFICATION, true)
var pendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
var uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
var mBuilder = NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID)
.setContentTitle(mContext.getString(R.string.app_name))
.setContentText(mFirstContactName + " : " + mListChatWindow[0].message)
.setPriority(if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) NotificationManager.IMPORTANCE_HIGH else Notification.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setSound(uri)
.setSmallIcon(R.drawable.ic_app_icon)
.setColor(ContextCompat.getColor(mContext, R.color.colorPrimary))
.setVibrate(longArrayOf(0, 100, 1000, 300))
.setAutoCancel(true)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
var channel = NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", NotificationManager.IMPORTANCE_HIGH)
channel.description = "NOTIFICATION_DESCRIPTION"
channel.lightColor = Color.LTGRAY
channel.enableVibration(true)
channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
val attributes = AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build()
channel.setSound(uri, attributes)
var notificationManager = mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
var notificationManager = NotificationManagerCompat.from(mContext)
notificationManager.notify(NOTIFICATION_CHAT_ID, mBuilder.build())
I am also set channel.setSound(uri, attributes) in cannel but sound not coming
Here is the screenshot of Notification channel see there sound icon is disabled, how to enable?
Plz help
I'm facing same issue and still didn't get satisfying answer, but till that time we can workaround it like so:
final Uri NOTIFICATION_SOUND = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
RingtoneManager.getRingtone(context, NOTIFICATION_SOUND).play();
You need to call this after:
notificationManager.notify(notificationId, notification);
This way you will always get sound playing even if "allow sound" was turned off for your App, and the sound played will be from the system not media (expected behavior from the notifications).
And to avoid having two sounds playing at once (for the devices that don't have this issue), you can turn the sounds off like so:
1- For the Builder:
notificationBuilder.setContentTitle(title)
.set.....
.set.....
.setSound(null);
2- For the channel:
channel.setSound(null, null);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With