我想在服务器向我发送一些信息时显示一些通知。有时我可以在通知有效负载中接收 HTML,但我无法在通知描述中显示它。我在这里处理传入的数据:
override fun onMessageReceived(remoteMessage: RemoteMessage) {
if (remoteMessage.notification != null) {
sendNotification(remoteMessage)
}
}
这里:
private fun sendNotification(messageBody: RemoteMessage) {
val intent = Intent(this, HomeScreen::class.java)
intent.putExtra("push_model", messageBody)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
var receivedModel: Any? = null
when (messageBody.data["notification_type"]) {
"messagecenter" -> receivedModel =
Gson().fromJson(messageBody.data["payload"], ViewMessage::class.java)
}
var title = ""
var message = ""
if (receivedModel is ViewMessage) {
val messageN: ViewMessage = receivedModel
title = messageN.subject.toString()
message =
messageN.body?.let { HtmlCompat.fromHtml(it, HtmlCompat.FROM_HTML_MODE_LEGACY) }
.toString()
intent.apply {
putExtra("action_id", 1)
putExtra("m_id", messageN.id)
putExtra("list_type", 0)
putExtra("list_pos", 0)
}
}
val pendingIntent = PendingIntent.getActivity(
this, 0, intent,
PendingIntent.FLAG_ONE_SHOT
)
val channelId = "TEst channel"
val defaultSoundUri: Uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder: NotificationCompat.Builder =
NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_docs_icon)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
val channel = NotificationChannel(
channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_HIGH
)
notificationManager.createNotificationChannel(channel)
with(NotificationManagerCompat.from(this)) {
notify(SystemClock.uptimeMillis().toInt(), notificationBuilder.build())
}
}
如您所见,我尝试在此处处理 html:
message = messageN.body?.let { HtmlCompat.fromHtml(it, HtmlCompat.FROM_HTML_MODE_LEGACY) }.toString()
但最后我在通知中看到了未处理的数据。然后我试图通过删除 .setContentText(message)
来完全隐藏通知描述,但我仍然在通知中看到未处理的 html。
另一个奇怪的,这与意图处理有关。如您所见,我已将一些数据添加到附加内容中。在处理后的目标活动中,我尝试打开一些片段。因此,我的片段不会在真实设备上以这种方式打开。我无法理解会发生什么,因为我在模拟器上对其进行了测试,并且可以在接收有效载荷并对其进行处理后打开片段。也许我做错了什么?