我正在尝试创建来电推送通知。发生呼叫事件时,带有通知的前台服务将启动。我为此创建了一个频道和通知。这是代码:
频道设置:
private fun createCallChannelChannel(): NotificationChannel {
val attributes = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build()
val importance = NotificationManager.IMPORTANCE_HIGH
return NotificationChannel(CALL_CHANNEL_ID, CALL_CHANNEL_NAME, importance).apply {
description = CALL_CHANNEL_DESCRIPTION
setSound(
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE),
attributes
)
enableLights(true)
enableVibration(true)
}
}
通知设置:
private fun buildIncomingCallNotification(payload: VoIpCallResponse): Notification {
val remoteView = RemoteViews(packageName, R.layout.notification_call_view)
remoteView.setOnClickPendingIntent(R.id.declineBtn, getDeclinePendingIntent())
remoteView.setOnClickPendingIntent(R.id.acceptBtn, getAcceptPendingIntent(payload))
return NotificationCompat.Builder(this, CALL_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setStyle(NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(remoteView)
.setAutoCancel(false)
.build()
}
有效。显示通知。但是问题是几秒钟后通知最小化到通知栏。目的是防止通知最小化,直到用户拒绝/接受呼叫或结束呼叫事件发生为止。例如WhatsApp。来电通知会在屏幕顶部无限期停留。我该怎么做?我的频道的重要性是NotificationManager.IMPORTANCE_HIGH,通知优先级是NotificationCompat.PRIORITY_MAX
答案 0 :(得分:0)
我已经找到此页面,并且该页面正常工作
https://developer.android.com/training/notify-user/time-sensitive
val fullScreenIntent = Intent(this, CallActivity::class.java)
val fullScreenPendingIntent = PendingIntent.getActivity(this, 0,
fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT)
val notificationBuilder =
NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Incoming call")
.setContentText("(919) 555-1234")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_CALL)
// Use a full-screen intent only for the highest-priority alerts where you
// have an associated activity that you would like to launch after the user
// interacts with the notification. Also, if your app targets Android 10
// or higher, you need to request the USE_FULL_SCREEN_INTENT permission in
// order for the platform to invoke this notification.
.setFullScreenIntent(fullScreenPendingIntent, true)
当然也可以与前景服务一起使用
val incomingCallNotification = notificationBuilder.build()