android - 当应用程序处于后台状态时,通知 PendingIntent 不起作用
问题描述
更新!!!
我解决了这个问题。问题出在 remoteMessage.notification.let { 仅在应用程序处于前台状态时触发,因此我删除了这部分,它就像一个魅力一样工作)
当我发送推送通知并且当应用程序处于后台状态时,通知即将到来但我点击它时没有做任何事情。但是当应用程序处于前台时,点击会再次进入应用程序。
这是来自 FirebaseMessagingService 的一些代码
remoteMessage.notification.let { val notificationManager = getSystemService(NOTIFICATION_SERVICE) 作为 NotificationManager
val intent = Intent(this, MainActivity::class.java)
.putExtra(CLICK_ACTION, mapData.get("click_action"))
.putExtra(PAGE_TO_GO, mapData.get("pagetogo"))
.putExtra(NOTIFIATION_TITLE, mapData.get("title"))
.putExtra(NOTIFICATION_BODY, mapData.get("body"))
.putExtra(COMPANY_ID, mapData.get("company_id"))
.putExtra(DOCUMENT_ID, mapData.get("document_id"))
.putExtra(ORDER_ID, mapData.get("order_id"))
.putExtra(FROM_DATE_EXTRA, mapData.get("from_date"))
.putExtra(TO_DATE_EXTRA, mapData.get("to_date"))
.putExtra(LEDGER_VIEW_ID_EXTRA, mapData.get("id_for_ledge_view"))
.putExtra(IS_CREDIT_EXTRA, mapData.get("is_credit"))
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
val pendingIntent = PendingIntent.getActivity(this, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT)
val notification = NotificationCompat.Builder(this, getString(R.string.channel_id))
.setSmallIcon(R.drawable.ic_baseline_notifications_24)
.setContentTitle(it?.title)
.setContentText(it?.body)
.setContentIntent(pendingIntent)
.setStyle(
NotificationCompat.BigTextStyle()
.bigText(it?.body)
)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true)
with(notificationManager) {
notify(NOTIFICATION_ID, notification.build())
}
解决方案
我解决了这个问题。问题出在 remoteMessage.notification.let { 仅在应用程序处于前台状态时触发,因此我删除了这部分,它就像一个魅力)