所以我知道这可能没有帮助,但它“神奇地”从今天开始起作用。浏览器版本为三星互联网 v10。
firebase-messaging-sw.js
// Give the service worker access to Firebase Messaging.
// Note that you can only use Firebase Messaging here, other Firebase libraries
// are not available in the service worker.
importScripts('https://www.gstatic.com/firebasejs/7.13.2/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/7.13.2/firebase-messaging.js');
// Initialize the Firebase app in the service worker by passing in
// your app's Firebase config object.
// https://firebase.google.com/docs/web/setup#config-object
firebase.initializeApp({
apiKey: '',
authDomain: '',
databaseURL: '',
projectId: '',
storageBucket: '',
messagingSenderId: '',
appId: '',
measurementId: ''
});
// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(payload => {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Customize notification
const notificationTitle = payload.data.title;
const notificationOptions = {
body: payload.data.body,
priority: payload.data.priority,
vibrate: payload.data.vibrate,
icon: payload.data.icon,
click_action: payload.data.link,
link: payload.data.link
};
return self.registration.showNotification(notificationTitle,
notificationOptions);
});
//Open browser window or focus it if it is open on notification click
self.addEventListener('notificationclick', function(event) {
event.notification.close();
event.waitUntil(self.clients.openWindow('www.yourwebsitehere.com'));
});
发送通知的云功能
//Sends notifications to users when the statistics document is updated
exports.sendNotifications = functions.firestore.document('restaurant/statistics').onUpdate(async (snapshot,context) =>{
//Get updated document data
const updatedData = snapshot.after.data();
const payload = admin.messaging().Message = {
data: {
title: updatedData.title,
body : updatedData.body,
icon: updatedData.icon,
link: updatedData.link,
vibrate: "[300, 200, 300]",
priority: "high"
},
topic: 'statistics'
}
return admin.messaging().send(payload);
});