我已将基于 FCM 的推送通知集成到我的 Angular 应用程序中,推送通知会在 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 的支持?
最佳答案
对于在您的 Mac 上没有收到推送通知的任何人,请确保您已在“系统偏好设置”->“通知”->“Chrome”中为 Chrome 启用“通知”
即使我只是简单地从 Firebase 教程中复制和粘贴代码,我也在为这个问题而苦苦挣扎:)
关于angular - chrome (macOs) 不显示桌面推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55234458/