我使用 firebase 进行通知处理,我想要带有 TEST_ACTION 的通知,这是我在有效负载中的可点击操作,将我发送到我的 RecognizeUserActivity,除了一个问题之外,一切正常,当我将应用程序放在后台时,同时收到带有 TEST_ACTION 的通知当我恢复应用程序时,它会将我发送到我的 RecognizeUserActivity,但我想要的是仅在单击下拉菜单中的通知时发送给我。 这是我的 firebaseMessage 类
private fun sendNotification(message: RemoteMessage) {
val title = message.notification?.title.toString()
val body = message.notification?.body.toString()
val notificationIntent: Intent = if (message.notification?.clickAction.toString() == "TEST_ACTION") {
Intent(this, RecognizeUserActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
putExtra("message", body)
}
} else {
Intent(this, MainActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
putExtra("message", body)
}
}
val requestCode = System.currentTimeMillis().toInt()
val pendingIntent: PendingIntent = PendingIntent.getActivity(this, requestCode, notificationIntent, PendingIntent.FLAG_IMMUTABLE)
val channelId = "APP"
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder: NotificationCompat.Builder =
NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.icon)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.setStyle(NotificationCompat.BigTextStyle())
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
channelId,
"AUTHORIZATION NOTIFICATIONS",
NotificationManager.IMPORTANCE_DEFAULT
)
notificationManager.createNotificationChannel(channel)
}
val notificationId = System.currentTimeMillis().toInt()
notificationManager.notify(notificationId, notificationBuilder.build())
}
override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
sendNotification(message)
}
override fun handleIntent(intent: Intent) {
try {
if (intent.extras != null) {
val builder = RemoteMessage.Builder("FirebaseMessagingService")
for (key in intent.extras!!.keySet()) {
builder.addData(key!!, intent.extras?.getString(key))
}
onMessageReceived(builder.build())
} else {
super.handleIntent(intent)
}
} catch (e: Exception) {
super.handleIntent(intent)
}
}
已解决,根本不是通知问题。