【发布时间】:2020-10-06 18:57:51
【问题描述】:
我正在使用 FCM 显示推送通知,但是当应用程序在后台运行或关闭时,通知不显示操作。
我收到消息后收到消息并使用NotificationCompat.Builder 构建通知
val notification: Notification =
NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
.setContentTitle(message.notification?.title ?: "Title")
.setContentText(message.notification?.body ?: "Body")
.setStyle(
NotificationCompat.BigTextStyle()
.bigText(message.notification?.body ?: "Grant access?")
)
.addAction(getApproveAction(message.data["pushId"] ?: ""))
.addAction(getRejectAction(message.data["pushId"] ?: ""))
.setSmallIcon(R.drawable.logo)
.build()
val manager = NotificationManagerCompat.from(applicationContext)
manager.notify(NOTIFICATION_ID, notification)
动作方法看起来非常相似,唯一的区别是动作和ACCEPTED 布尔值。批准操作如下所示:
private fun getApproveAction(pushId: String): NotificationCompat.Action {
val approveIntent =
Intent(this, NotificationActionReceiver::class.java).setAction(getString(R.string.notification_action_approve))
.apply {
putExtra(PUSH_ID, pushId)
putExtra(ACCEPTED, true)
}
val approvePendingIntent: PendingIntent =
PendingIntent.getBroadcast(this, 1, approveIntent, PendingIntent.FLAG_CANCEL_CURRENT)
return NotificationCompat.Action(R.drawable.done, "Accept", approvePendingIntent)
}
当应用程序处于前台时,通知会通过两个操作完美显示。但是当应用程序在后台运行或关闭时,我只看到标题和正文,没有任何操作。我正在使用 Android 10 在像素 3a 模拟器上进行测试。
我已尝试将 Intent 格式更新为此处描述的 https://stackoverflow.com/a/47032464/4801470,但没有任何运气。
【问题讨论】:
标签: android firebase firebase-cloud-messaging android-notifications