Firebase - 不接收有效负载的notificationclick事件

nni*_*mis 6 push push-notification firebase service-worker

我在这个问题上挣扎了一个多星期......我们已经使用Firebase和Service Worker在Chrome中实现了推送消息.一切正常,消息正在使用有效负载正确发送和接收.在服务工作者上,我们处理推送消息以显示通知和notificationclick事件,以便在单击时将用户发送到特定URL,然后关闭通知.

问题在于"旧"通知:如果用户收到消息但没有立即点击它,它会保留一段时间,然后在一段时间后(不确定多少)他点击通知 - 他被重定向到https:// [domain] /firebase-messaging-sw.js 我们已经跟踪了整个过程:收到的通知都包含了所有信息(网址也是正确的,实际上如果他在收到邮件时点击了它就可以了正好).但如果通知在那里存在一段时间就会被"清空".

发送的消息非常简单(仅用于显示没有TTL,也没有使用过期参数).curl命令看起来像这样:

curl -X POST 
    -H "Authorization: key=[SERVER API KEY]"
    -H "Content-Type: application/json"
    -d '{
    "notification":{
        "click_action":"[URL to be redirected]",
        "icon":"[A custom image]",
        "title":"[News title]"},
        "to":"/topics/[A topic]"
    }' 
"https://fcm.googleapis.com/fcm/send"
Run Code Online (Sandbox Code Playgroud)

这是用于处理服务工作者上的推送消息的代码:

self.addEventListener('push', function(event) {
    console.log('Push message received', event);
    var data = {};
    if (event.data) {
        data = event.data.json();
    }
    event.stopImmediatePropagation();
    showNotification(data.notification);
});

function showNotification(notification) {
    var click_action = notification.click_action; //<-- This is correct!
    var options = {
        body: notification.body,
        icon: notification.icon,
        subtitle: notification.subtitle,
        data: {
            url: click_action
        }
    };
    if (self.registration.showNotification) {
        return self.registration.showNotification(notification.title, options);
    }
}
Run Code Online (Sandbox Code Playgroud)

管理notificationclick事件的代码非常简单:

self.addEventListener('notificationclick', function(event) {
    var url = '';
    try {
        url = event.notification.data.url; // <-- event.notification is null randomly!!
    } catch (err) {}
    event.waitUntil(self.clients.openWindow(url));
});
Run Code Online (Sandbox Code Playgroud)

是否有任何理由在服务工作者上下文一段时间后丢失有效负载?这个时间是否在任何文档中指定?在此先感谢您的帮助!

小智 2

//payload of push message
{
"notification": {
                    "title": data.title,
                    "body": data.text,
                    "click_action": url,
                    "tag": "",
                    "icon": ""
                },
                "to": token             
}

//in service woker
self.addEventListener("notificationclick", (event) => {
    event.waitUntil(async function () {
        const allClients = await clients.matchAll({
            includeUncontrolled: true
        });
        let chatClient;
        for (const client of allClients) {
            if (client['url'].indexOf(event.notification.data.FCM_MSG.notification.click_action) >= 0) {
                client.focus();
                chatClient = client;
                break;
            }
        }
        if (!chatClient) {
            chatClient = await clients.openWindow(event.notification.data.FCM_MSG.notification.click_action);
        }
    }());
});
Run Code Online (Sandbox Code Playgroud)

因此,基本上,在单击通知时,您将被重定向到应用程序,如果应用程序未打开,您将在新选项卡中打开应用程序,因为您担心如果无法获得 click_action,您可以尝试event.notification.data.FCM_MSG.notification.click_action通过使用它并查看是否获取 url,并在服务工作人员https://github.com/firebase/quickstart-js/issues/102的开头添加事件侦听器

  • 谢谢 ravi,但是 event.notification.data.FCM_MSG 未定义 (3认同)