首页 > 解决方案 > Chrome 扩展的 GCM 到 FCM

问题描述

我一直在使用 Gmail、Calendar 和 Drive API通过 chrome.gcm.onMessage 在我的 Chrome 扩展程序中获取推送通知

但是 chrome.gcm 已被弃用,我假设 chrome.gcm.onMessage 将在 4 月停止工作。有没有替代的解决方案?我正在尝试将 Firebase JS 客户端用作服务人员,我正在向 setBackgroundMessageHandler 接收消息,但它们正在强制显示桌面通知!我想自己处理数据消息,就像我处理 chrome.gcm.onMessage 一样

清单.json

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

背景.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);
  });

标签: firebasegoogle-chromegoogle-cloud-messaging

解决方案


推荐阅读