FCM Web发送消息成功,但浏览器中无任何显示

时间:2019-03-15 10:51:41

标签: javascript php firebase firebase-cloud-messaging web-notifications

我正在发送以下通知

Authorization:key=A.....
Content-Type:application/json    
{
    "to" : "cBx_ErhkhNw:APA91bEzwHvZ2daEK15Y3A0VAprxPV-4GGq10FjlrFnHzCs8xSJXz2lATjh9AoLRQvnSseT6r4ggApC6TbtD9M_sI_BDDmycZ1TEVn0HaF5pAABeSbHqcOK-kmkPqOve1VThUaUIXQy4",

      "notification": {
        "title": "Test Nachricht",
        "body": "This is a message from FCM to web"
      },
      "priority": "high",
      "webpush": {
      "fcm_options": {
        "link": "https://dummypage.com"
      }
    }

}

并具有以下代码:

app.js

 // Initialize Firebase
 var config = {
     apiKey: "<KEY>",
     authDomain: "<AUTH_DOMAIN>",
     databaseURL: "<DB_URL>",
     projectId: "atlas-test-35d33",
     storageBucket: "<STORAGE_BUCKET>",
     messagingSenderId: "817272374311"
 };
 firebase.initializeApp(config);
 const messaging = firebase.messaging();
 messaging.usePublicVapidKey("<PUBLIC_KEY>");
 messaging.requestPermission()
     .then(function() {
         console.log("Permission granted");
         return messaging.getToken()
     })
     .then(function(token) {
         console.log(token)
     })
     .catch(function(err) {
         console.log(err)
     })
 console.log("listenting")
 messaging.onMessage(function(payload) {
     console.log('Message received. ', payload);
     // ...
 });

firebase-messaging-sw.js

    // Give the service worker access to Firebase Messaging.
// Note that you can only use Firebase Messaging here, other Firebase libraries
// are not available in the service worker.
importScripts('https://www.gstatic.com/firebasejs/5.9.0/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/5.9.0/firebase-messaging.js');

  var config = {
    apiKey: "<KEY>",
    authDomain: "<AUTH_DOMAIN>",
    databaseURL: "https://atlas-test-35d33.firebaseio.com",
    projectId: "atlas-test-35d33",
    storageBucket: "<STORAGE_BUCKET>",
    messagingSenderId: "817272374311"
  };
  firebase.initializeApp(config);

// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();

当我尝试向fcm服务器发送通知时,得到以下响应:

{
    "multicast_id": 5394725759940263108,
    "success": 1,
    "failure": 0,
    "canonical_ids": 0,
    "results": [
        {
            "message_id": "0:1552646324437940%e609af1cf9fd7ecd"
        }
    ]
}

似乎一切对我都很好。但是我的Web应用程序没有收到任何信息。我只是将“ to”令牌硬编码用于测试目的,但我不认为这就是问题所在。

您是否有任何问题的线索,或者是否有调试的可能性。

谢谢!

0 个答案:

没有答案