从GCM到FCM的Chrome扩展程序

时间:2019-03-23 20:20:21

标签: firebase google-chrome google-cloud-messaging

我一直在使用Gmail,日历和云端硬盘API通过chrome.gcm.onMessage在Chrome扩展程序中获取push notifications

但是chrome.gcm正在deprecated,我假设chrome.gcm.onMessage将在4月停止工作。有没有其他解决方案?我正在尝试将Firebase JS客户端用作服务工作者,正在将消息发送到setBackgroundMessageHandler,但是它们正在强制显示桌面通知!我想自己像处理chrome.gcm.onMessage

一样处理数据消息

manifest.json

"manifest_version": 2,
...
"gcm_sender_id": "103953800507"
...

background.js

navigator.serviceWorker.register('js/firebase-messaging-sw.js')

// Also how does this all play in now with FCM...
chrome.instanceID.getToken({
   authorizedEntity: [MY_GCM_SENDER_ID],
   scope: "GCM"
}).then(token => {

});

js / firebase-messaging-sw.js

importScripts('https://www.gstatic.com/firebasejs/5.9.1/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/5.9.1/firebase-messaging.js');

firebase.initializeApp({
    messagingSenderId: [MY_GCM_SENDER_ID] // Same I used to use with chrome.instanceID.getToken I guess???
});

const messaging = firebase.messaging();

messaging.setBackgroundMessageHandler(function(payload) {
    console.log('[firebase-messaging-sw.js] Received background message ', payload);
    // Customize notification here
    var notificationTitle = 'Background Message Title';
    var notificationOptions = {
      body: 'Background Message body.',
      icon: '/images/icon128.png'
    };

    return self.registration.showNotification(notificationTitle,
      notificationOptions);
  });

0 个答案:

没有答案