我试图让我的“click_action”将用户带到我发送给客户的通知上的特定 URL,但无论我做什么(桌面端)都没有反应或只是打开了 PWA(Android)。消息通过得很好(在 Chrome 控制台中检查),但似乎点击不起作用。
以下是我在服务工作者中使用的代码,摘自各处,包括本网站提供的其他答案:
在安卓设备(已安装PWA)和Chrome浏览器上,通知可以正常接收,并且开发者控制台中的消息载荷格式良好且接收正常。在从服务器发送的消息中,我有一个带有自定义参数的URL(例如,https://[domain]/list.php?userid=123),但是与上述情况相同,单击通知在Windows/Chrome上不起作用,在安卓设备上成功打开PWA,但随后不会转到载荷中的URL,而是转到了上次打开PWA时的位置。 "userid" 根据消息触发器而变化。
以下是我在服务工作者中使用的代码,摘自各处,包括本网站提供的其他答案:
importScripts('https://www.gstatic.com/firebasejs/7.14.3/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/7.14.3/firebase-messaging.js');
// importScripts('/__/firebase/init.js');
/* An empty service worker! */
self.addEventListener('fetch', function(event) {
/* An empty fetch handler! */
});
var firebaseConfig = {
//REDACTED
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function(payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Customize notification here
notificationTitle = payload.notification.title;
notificationOptions = {
body: payload.notification.body,
icon: payload.notification.icon,
click_action: payload.notification.click_action
};
return self.registration.showNotification(notificationTitle,
notificationOptions);
});
self.addEventListener('notificationclick', function(event) {
let url = event.notification.click_action;
// I've also added a data.click_action field in my JSON notification, and have tried using that
// instead, but that didn't work either
console.log('On notification click: ', event.notification.tag);
event.notification.close(); // Android needs explicit close.
event.waitUntil(
clients.matchAll({ includeUncontrolled: true, type: 'window' }).then( windowClients => {
// Check if there is already a window/tab open with the target URL
for (var i = 0; i < windowClients.length; i++) {
var client = windowClients[i];
// If so, just focus it.
if (client.url === url && 'focus' in client) {
return client.focus();
}
}
// If not, then open the target URL in a new window/tab.
if (clients.openWindow) {
return clients.openWindow(url);
}
})
);
});
self.onnotificationclick = function(event) {
let url = event.notification.click_action;
console.log('On notification click: ', event.notification.tag);
event.notification.close();
// This looks to see if the current is already open and
// focuses if it is
event.waitUntil(clients.matchAll({ includeUncontrolled: true, type: 'window' }).then(function(clientList) {
for (var i = 0; i < clientList.length; i++) {
var client = clientList[i];
if (client.url == url && 'focus' in client)
return client.focus();
}
if (clients.openWindow)
return clients.openWindow(url);
}));
};
在安卓设备(已安装PWA)和Chrome浏览器上,通知可以正常接收,并且开发者控制台中的消息载荷格式良好且接收正常。在从服务器发送的消息中,我有一个带有自定义参数的URL(例如,https://[domain]/list.php?userid=123),但是与上述情况相同,单击通知在Windows/Chrome上不起作用,在安卓设备上成功打开PWA,但随后不会转到载荷中的URL,而是转到了上次打开PWA时的位置。 "userid" 根据消息触发器而变化。
消息负载的JSON示例:
{data: {…}, from: "xxx", priority: "high", notification: {…}, collapse_key: "do_not_collapse"}
collapse_key: "do_not_collapse"
data: {gcm.notification.badge: "[logo URL]", click_action: "https://[URL]/list.php?userid=33"}
from: "xxx"
notification:
body: "'5' has just been added"
click_action: "https://[URL]/list.php?userid=33"
icon: "https://[logo URL]"
title: "alert "
我在https://firebase.google.com/docs/cloud-messaging/js/receive上看到了一些关于"webpush": {"fcm_options": {"link": "https://dummypage.com"}}的内容,但我无法确定它是否相关或是否也需要。
仅在click_action中提供URL点击通知时似乎并未执行该操作,这让我非常惊讶!在服务工作者中是否需要任何内容?!?!
问题之一可能是PWA不会定期更新SW,因此如果我上面的代码应该可以工作(大前提!),那么我只需要等待已安装的Android应用程序更新SW即可吗?如果是这样,有没有加快其更新速度的方法?
非常感谢您提前提供任何帮助。 这里让我非常困扰!