谷歌chrome扩展中的Firebase云消息



我目前正在开发一个谷歌chrome扩展,需要在其中接收推送通知。为此,我使用Firebase Cloud消息传递。

令牌发送正确,我可以通过邮递员发送通知,我在谷歌chrome上收到通知。

然而,onMessage和setBackgroundMessageHandler从来都不是触发器,就像通知是完全独立的。

这是我的代码:

background.js

chrome.runtime.onInstalled.addListener(function() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('../firebase-messaging-sw.js');
} else {
console.warn('Service workers aren't supported in this browser.');
}
firebase.initializeApp({
apiKey: "xx",
authDomain: "xx",
databaseURL: "xx",
projectId: "xx",
storageBucket: "xx",
messagingSenderId: "xx",
appId: "xx"
});
const messaging = firebase.messaging();
messaging.usePublicVapidKey("xx");
// WORK WELL
messaging.requestPermission()
.then(function() {
console.log("=== have permission ===");
return messaging.getToken();
})
.then(function(currentToken) {
console.log("Set token : ", currentToken);
chrome.storage.sync.set({fcmToken: currentToken}, function() {});
})
.catch(function(err) {
console.log("==== error ====", err);
});
messaging.onTokenRefresh(() => {
messaging.getToken().then((refreshedToken) => {
console.log("Refresh token : ", refreshedToken);
chrome.storage.sync.set({fcmToken: refreshedToken}, function() {});
}).catch((err) => {
console.log('Unable to retrieve refreshed token ', err);
});
});
//Never trigger 
messaging.onMessage((payload) => {
chrome.browserAction.setBadgeText({text: "1"});
console.log('Message received. ', payload);
});

firebase消息收发软件.js

importScripts('https://www.gstatic.com/firebasejs/7.14.0/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/7.14.0/firebase-messaging.js');
firebase.initializeApp({
apiKey: "xx",
authDomain: "xx",
databaseURL: "xx",
projectId: "xx",
storageBucket: "xx",
messagingSenderId: "xx",
appId: "xx"
});
const messaging = firebase.messaging();
// NEVER TRIGGER
messaging.setBackgroundMessageHandler(function(payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
const notificationTitle = 'Background Message Title';
const notificationOptions = {
body: 'Background Message body.',
icon: '/firebase-logo.png'
};
return self.registration.showNotification(notificationTitle,
notificationOptions);
});

manifest.json

{
"manifest_version": 2,
"name": "app",
"description": "app",
"version": "0.0.3",
"permissions": [
"http://*/*",
"https://*/*",
"storage",
"notifications"
],
"web_accessible_resources": [
"js/popup.js",
"css/popup.css",
"html/popup.html",
"html/background.html",
"js/background.js"
],
"background": {
"page": "html/background.html",
"persistent": false
},
"options_page": "html/options.html",
"browser_action": {
"default_title": "Notification",
"default_popup": "html/popup.html",
"default_icon": {
"128": "img/get_started128.png"
}
},
"content_security_policy": "script-src 'self' https://www.gstatic.com/ https://*.firebaseio.com https://www.googleapis.com; object-src 'self'",
"icons": {
"128": "img/get_started128.png"
}

}

如果你有答案,非常感谢!

祝你今天过得愉快,Gabin

onMessagesetBackgroundMessageHandler仅在从后台而非前台接收消息时触发。

在文档中,它提到你可以在两个方面接收消息:前台和后台。

如果你想记录/查看来自服务人员的推送消息,你可以使用:

this.push(event)=>{
console.log(event.data)
}

相关内容

  • 没有找到相关文章