firebase - 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);
});
解决方案
推荐阅读
- android - Android AWS TransferService 在 API 级别 26 及更高版本中不起作用
- java - 为什么输出在下面的java代码中?
- mysql - 如何找到曾经相同但现在已损坏的两张表之间的差异
- boost-log - Boost Log 轮换文件名模式整数从未重新启动
- python-3.x - Kivy Screenmanager 和 RecycleView 的滞后
- c++ - 无法迭代具有结构“字符串”的哈希图:[list] C++
- python - 使用 Python 中的 package_create API 调用创建包含资源的包
- amazon-cloudformation - 在创建弹性搜索域时,我可以将 aws cognito 用于 cloudformation 的 kibana 吗?
- python - 我在使用 Python pip 安装 Pyhook 时遇到错误“无法导入 main”
- ruby - 如何找到到给定位置的线串的最近点 - rgeo