【发布时间】:2020-02-06 08:00:02
【问题描述】:
我像这样使用云功能发送 FCM
const payload = {
data: {
imagePath: imagePath,
priority: priority,
title: title,
body: body
}
}
await admin.messaging().sendToDevice(token,payload)
这是我的 Android 应用程序上的代码,在 onMessageReceived 中,下面的这个 onMessageReceived 代码实际上被调用了,但我不知道为什么通知没有显示在我的设备中
override fun onMessageReceived(remoteMessage: RemoteMessage) {
super.onMessageReceived(remoteMessage)
var title: String = ""
var body: String = ""
var priority: String = ""
var imagePath: String = ""
remoteMessage.notification?.let {
title = it.title ?: ""
body = it.body ?: ""
}
val notificationData = remoteMessage.data
priority = notificationData["priority"] ?: ""
imagePath = notificationData["imagePath"] ?: ""
title = notificationData["title"] ?: ""
body = notificationData["body"] ?: ""
setUpNotification(title,body,priority,imagePath)
}
并向设备显示通知
private fun setUpNotification(title: String, message: String, priority: String, imagePath: String) {
val channel = if (priority == "high") NOTIFICATION_CHANNEL_IMPORTANT else NOTIFICATION_CHANNEL_MISCELLANEOUS
var notification = NotificationCompat.Builder(this, channel)
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentTitle(title)
.setContentText(message)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.build()
notificationManager.notify(1, notification)
}
但是如果我像这样在有效负载上添加通知消息(不仅仅是数据消息)
const payload = {
notification: {
body: body,
title: title,
},
data: {
imagePath: imagePath,
priority: priority,
title: title,
body: body
}
}
然后通知将显示在我的设备上
【问题讨论】:
-
在
setUpNotification方法中传递静态随机数据检查这个方法是否完美工作。 -
@frankenstein 是的,它也被调用了,但我不知道为什么它不显示
-
这个方法有问题
-
@Alexa289 你解决了吗?
-
@Black_Bacardi 是的,就我而言,问题根本不在于 FCM,而在于我在 Android 中创建频道的方式。你可以看到这个stackoverflow.com/questions/60105947/…
标签: android firebase google-cloud-functions firebase-cloud-messaging