如何解决此问题:当我关闭索引支付时,firebase-messaging-sw.js无法收到通知

时间:2019-04-17 05:35:02

标签: javascript firebase firebase-cloud-messaging

我使用fcm编写了一些代码,但是如果我打开index.php,它将成功接收通知,但是当我关闭index.php时,它将无法接收任何通知。 我该如何解决? service worker is ok notification when i open index.php

//firebase-messaging-sw.js
importScripts('https://www.gstatic.com/firebasejs/5.9.4/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/5.9.4/firebase-messaging.js');
var config = {
    apiKey: "...",
    authDomain: "...",
    databaseURL: "...",
    projectId: "...",
    storageBucket: "...",
    messagingSenderId: "..."
  };
  firebase.initializeApp(config);
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function(payload) {
  console.log('[firebase-messaging-sw.js] Received background message ', payload);
  // Customize notification here
  var notificationTitle = payload.data.title;
  var notificationOptions = {
    body: payload.data.body,
    icon:'http://localhost/test_notification/img/icon.png',
    image:'http://localhost/test_notification/img/d.png'
  };
  return self.registration.showNotification(notificationTitle,
    notificationOptions);
});

0 个答案:

没有答案