I am trying to implement audio/video calls notification, like it works in Telegram. I have a problem with vibration. It does not stop after cancelling notification and after timeout. A sound stops. Only rebooting my phone helps. I tried lot's of things. I want my vibration pattern to repeat until the notification is cancelled. Here is my code for creating the channel:
fun createCallChannel(context: Context, channelID: String){
val callChannel = NotificationChannel(
channelID,
"Call",
NotificationManager.IMPORTANCE_HIGH // for heads-up notifications
)
callChannel.description = "call channel"
callChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
val audioAttributes = AudioAttributes.Builder()
.setLegacyStreamType(AudioManager.STREAM_RING)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.setUsage(AudioAttributes.USAGE_MEDIA)
.build()
// when i use this commented line, vibrations does not work at all. Why?
// val audioAttributes = AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE).build()
val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE)
callChannel.setSound(soundUri, audioAttributes)
callChannel.enableVibration(true)
val vibrationPattern = LongArray(2) { 500 }
callChannel.vibrationPattern = vibrationPattern
val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
manager.createNotificationChannel(callChannel)
}
Here is my code for creating the notification:
val notificationBuilder: NotificationCompat.Builder =
NotificationCompat.Builder(this, ChannelsUtils.CHANNEL_CALL_ID)
.setSmallIcon(R.drawable.ic_push_notification)
.setColor(rootAccentColor)
.setContentTitle(getString(contentTitleId))
.setContentText(fio)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE))
.setDefaults(Notification.DEFAULT_ALL)
.setOngoing(true)
.setAutoCancel(false)
.setTimeoutAfter(60 * 1000)
.addAction(
0,
getString(R.string.answer), answerCallPendingIntent
)
.addAction(
0,
getString(R.string.cancel), declineCallPendingIntent
)
.setFullScreenIntent(fullScreenPendingIntent, true)
val notification = notificationBuilder.build()
notification.flags = notification.flags or Notification.FLAG_INSISTENT// i use this flag to repeat sound and vibration
notificationManager.notify(
NotificationBroadcastReceiver.CALL_NOTIFICATION_ID,
notification
)
This way i cancel the notification:
private val notificationManager by lazy { getSystemService(NotificationManager::class.java) }
notificationManager.cancel(NotificationBroadcastReceiver.CALL_NOTIFICATION_ID)
Please help.