使用 firebase 向安卓设备发送推送通知。
<service
android:name=".util.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
class MyFirebaseMessagingService: FirebaseMessagingService() {
private val TAG = "FirebaseMessaging"
override fun onNewToken(token: String) {
super.onNewToken(token)
Log.d(TAG, token)
}
override fun onMessageReceived(remoteMessage: RemoteMessage) {
super.onMessageReceived(remoteMessage)
if (remoteMessage.notification != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.notification!!.body)
}
}
}
我可以使用邮递员接收消息通知。
{
"registration_ids": ["KEY_HERE"],
"notification": {
"title": "HIMAN",
"body": "werwkejh"
}
}
这完美地工作,我可以添加一个断点并且通知通过。但是,我需要这是一个数据类型推送通知,因为当应用程序处于后台时它不会触发 onMessageReceived 回调 - 只是前台。它说它在邮递员中也很成功。
{
"multicast_id": 863107467701827657,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [
{
"message_id": "MESSAGE_ID"
}
]
}
数据推送通知的请求体如下:
{
"registration_ids": ["KEY_HERE"],
"data":{
"message": "hi!"
}
}
它只是没有触发我需要的 onMessageReceived 。有人可以帮忙吗?
如果您的通知包含"notification":
它会显示通知但当应用程序未运行时则onMessageReceived
不会调用。对于这种情况,您已在"data":
字段中传递数据并notification":
从您的响应中删除。
您的回复必须是这样的
{
"to": "your device token",
"data":{
"name": "John",
"message": "Hii! "
}
}
我有同样的问题,这对我有用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句