Я создал функцию показа уведомлений из любого места в моем приложении. Он работает нормально, но уведомления не создают звука, который вибрирует, даже когда я специально написал для него код. Я не тестировал его на старых телефонах, но он точно не работает на Android 9. Вот мой код:
fun displayLocalNotif(
context: Context,
notifId: Int,
tapOpenActivityClass: Class<*>,
channelName: String,
notifTitle: String,
notifText: String
) {
val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channelId = channelName.toLowerCase(Locale.ROOT).replace(" ", "_")
val intent = Intent(context, tapOpenActivityClass).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
val pendingIntent: PendingIntent = PendingIntent.getActivity(context, 0, intent, 0)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
channelId,
channelName,
NotificationManager.IMPORTANCE_DEFAULT
)
channel.lightColor = Color.WHITE
channel.enableLights(true)
val audioAttributes = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build()
channel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI, audioAttributes)
channel.enableVibration(true)
channel.setShowBadge(true)
notificationManager.createNotificationChannel(channel)
}
val builder = NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle(notifTitle)
.setContentText(notifText)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setVibrate(longArrayOf(1000, 1000))
.setContentIntent(pendingIntent)
.setAutoCancel(true)
with(NotificationManagerCompat.from(context)) {
notify(notifId, builder.build())
}
}
Я хочу, чтобы уведомления по умолчанию звучали, вибрировали и также отображались на экране блокировки по умолчанию. Как я могу это сделать?