【发布时间】:2020-07-01 08:16:04
【问题描述】:
我正在尝试创建来电推送通知。当呼叫事件发生时,带有通知的前台服务将启动。我为它和通知创建了一个频道。代码如下:
频道设置:
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
【问题讨论】:
标签: android foreground-service android-push-notification