我已经将基于FCM的推送通知集成到我的角度应用程序中,但ubuntu和Windows设备已触发了推送通知,但无法在macO上使用。我正在附上 firebase-messaging-sw.js 的代码:
importScripts('https://www.gstatic.com/firebasejs/4.8.1/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/4.8.1/firebase-messaging.js');
firebase.initializeApp({
'messagingSenderId': SENDER_ID
});
self.addEventListener('fetch', event => {
event.waitUntil(async function() {
// Exit early if we don't have access to the client.
// Eg, if it's cross-origin.
if (!event.clientId) return;
// Get the client.
const client = await clients.get(event.clientId);
// Exit early if we don't get the client.
// Eg, if it closed.
if (!client) return;
// Send a message to the client.
self.clients.matchAll().then(function(clients) {
clients.forEach(function(client) {
client.postMessage({
msg: "Hey I just got a fetch from you!",
url: event.request.url
});
});
});
}());
})
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function(payload) {
// Customize notification here
const notificationTitle = 'Background Message Title';
const notificationOptions = {
body: 'Background Message body.',
icon: '/firebase-logo.png'
};
return self.registration.showNotification(notificationTitle,
notificationOptions);
});
我们是否需要添加任何其他配置以添加对macOs Chrome的支持?
答案 0 :(得分:2)
如果任何人都没有在您的 Mac 上收到推送通知,请确保您已在系统偏好设置 -> 通知 -> Chrome 中启用 Chrome 通知
即使我只是简单地从 Firebase 教程中复制和粘贴代码,我也一直在努力解决这个问题:)
答案 1 :(得分:0)
取决于您发送通知的方式,您可能不需要执行任何其他消息传递事件处理,并且由于要添加显式侦听器,因此可能无法正常工作。我的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/6.6.1/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/6.6.1/firebase-messaging.js');
firebase.initializeApp({
messagingSenderId: 'SENDER_ID_HEREE',
});
firebase.messaging();
发送通知时,您可以使用服务器上的firebase admin sdk简单地发送这样的有效负载。 Firebase可以处理其他所有事情
var admin = require("firebase-admin");
// This is a specific account key file generated through the firebase UI
var serviceAccount = require("./serviceAccountKey.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "*DATABASE_URL*"
});
const payload = {
"token": "*DEVICE_TOKEN_HERE",
"notification": {
"title": "Test Title!",
"body": "Test Body."
},
"webpush": {
"fcm_options": {
"link": "*URL_TO_GO_TO_ON_NOTIFICATION_CLICK*"
},
"notification": {
"icon": "*image to show in the notification ( this path is relative to the site that is displaying the web notification )*"
}
}
}
admin.messaging().send(payload).then(res => {
console.log('SUCCESS ', res)
}).catch(err => {
console.log('uh roh ', err)
})