提问人:DorrisDormouse 提问时间:11/16/2023 更新时间:11/16/2023 访问量:31
IOS PWA FCM 通知 - onMessage 从不调用,onBackgroundMessage 始终调用
IOS PWA FCM notification - onMessage never called, onBackgroundMessage always called
问:
截至目前(11 月 23 日),我正试图弄清楚在 IOS 上的 PWA 中可以做什么和不能做什么。我已经开始测试 FCM 通知,在主页上安装了 PWA,并且我在操作系统中显示了通知 - 这感觉是一个很好的开始。但是,我注意到当应用程序打开时,这些通知不会触发页面上的 onMessage 事件,这意味着我无法在已打开的应用程序中更新显示。我在控制台上看不到任何错误。我还注意到 onBackgroundMessage 始终在 Service Worker 中被调用。代码全部基于 FCM 快速入门示例。
Service Worker 如下所示
// Import and configure the Firebase SDK
// These scripts are made available when the app is served or deployed on Firebase Hosting
// If you do not serve/host your project using Firebase Hosting see https://firebase.google.com/docs/web/setup
importScripts('./firebase-app-compat.js');
importScripts('./firebase-messaging-compat.js');
importScripts('./init.js');
const messaging = firebase.messaging();
// If you would like to customize notifications that are received in the
// background (Web app is closed or not in browser focus) then you should
// implement this optional method.
// Keep in mind that FCM will still show notification messages automatically
// and you should use data messages for custom notifications.
// For more info see:
// https://firebase.google.com/docs/cloud-messaging/concept-options
messaging.onBackgroundMessage(function(payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Customize notification here
const notificationTitle = 'Background Message Title';
const notificationOptions = {
body: 'Background Message body.',
icon: './firebase-logo.png'
};
self.registration.showNotification(notificationTitle,
notificationOptions);
});
页面上的脚本:
// Retrieve Firebase Messaging object.
const messaging = firebase.messaging();
var reg = null;
if ("serviceWorker" in navigator) {
window.addEventListener("load", function () {
navigator.serviceWorker.register("./firebase-messaging-sw.js").then(
function (registration) {
console.log(
"SW registration successful with scope: ",
registration.scope
);
},
function (err) {
console.log("SW registration failed: ", err);
}
);
});
navigator.serviceWorker.ready.then(registration => {
reg = registration;
resetUI();
});
}
// Handle incoming messages. Called when:
// - a message is received while the app has focus
// - the user clicks on an app notification created by a service worker
// `messaging.onBackgroundMessage` handler.
messaging.onMessage((payload) => {
console.log("Message received. ", payload);
// Update the UI to include the received message.
appendMessage(payload);
});
function resetUI() {
console.log("RESET UI");
clearMessages();
showToken("loading...");
// Get registration token. Initially this makes a network call, once retrieved
// subsequent calls to getToken will return from cache.
messaging
.getToken({
serviceWorkerRegistration:reg,
vapidKey:
"[vapidKey]",
})
.then((currentToken) => {
if (currentToken) {
sendTokenToServer(currentToken);
updateUIForPushEnabled(currentToken);
} else {
// Show permission request.
console.log(
"No registration token available. Request permission to generate one."
);
// Show permission UI.
updateUIForPushPermissionRequired();
setTokenSentToServer(false);
}
})
.catch((err) => {
console.log("An error occurred while retrieving token. ", err);
showToken("Error retrieving registration token. ", err);
setTokenSentToServer(false);
});
}
任何指针,这样我就可以在收到推送通知时更新显示,并且仅在应用程序未打开时才显示推送通知,这将很棒。
答: 暂无答案
评论