未收到Firebase消息

时间:2017-12-01 17:12:46

标签: javascript firebase web google-cloud-messaging firebase-cloud-messaging

我初始化Web firebase并得到令牌:

https://github.com/MarcinMoskala/KotlinAcademyApp/blob/master/web/src/main/web/js/initFirebase.js

我在创建后获得当前令牌,并以简单消息发送它:

curl -X POST -H "Authorization: key=AAAA_XXXMYKEYXXXw26gv" -H "Content-Type: application/json" -d '{
    "to" : "fo3aexgjsbQ:APA91bEWh47m2cVoflhQ_E__E31jbQpoyaHZTRKmkJIG7Uaarrw0KwvCYYCnNTMmaQIcKBFrq2fGFEBj-nVB9obE-wf4FiTCKedR_gWdMPfc8bNgpK5MQ7SsLmcVRWLlert3AXXdbuzk",
    "data" : {
      "body" : "This is an FCM notification message!",
      "title" : "FCM Message",
    }
}' "https://fcm.googleapis.com/fcm/send"

我得到以下成功结果:

{"multicast_id":8683406144829883570,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1512147661586241%2fd9afcdf9fd7ecd"}]}

问题是控制台中没有显示或打印任何内容!我发送请求,我看到成功并且......当网站打开和关闭时都没有发生任何事情!

这是我的firebase-messaging-sw.js文件放在根目录中:

https://github.com/MarcinMoskala/KotlinAcademyApp/blob/master/web/src/main/web/firebase-messaging-sw.js

我也试过这样设置配置:

firebase.initializeApp({
  'messagingSenderId': '1091715558873'
});

我指定了清单:

https://github.com/MarcinMoskala/KotlinAcademyApp/blob/master/web/src/main/web/manifest.json

1 个答案:

答案 0 :(得分:2)

由于您的有效负载使用密钥data,因此您发送的是data message,而不是notification。它们的处理方式不同,如explained in the documentation

data更改为notification

"to" : "fo3aexgjsbQ:APA91bEWh47m2cVoflhQ_E__E31jbQpoyaHZTRKmkJIG7Uaarrw0KwvCYYCnNTMmaQIcKBFrq2fGFEBj-nVB9obE-wf4FiTCKedR_gWdMPfc8bNgpK5MQ7SsLmcVRWLlert3AXXdbuzk",
"notification" : {
  "body" : "This is an FCM notification message!",
  "title" : "FCM Message",