Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase - notificationclick event not receiving payload

I'm struggling with this problem for more than a week... We have implemented push message in Chrome by using Firebase and a Service Worker. Everything works just fine, the messages are being sent and received correctly with the payload. On the service worker, we handle the push message to display a notification and the notificationclick event to send the user to a specific URL when clicking on it, then close the notification.

The problem is with 'old' notifications: if a user receives a message but doesn't clicks on it right away, it keeps there for a while then after some time (not sure how much) he clicks the notification - he gets redirected to https://[domain]/firebase-messaging-sw.js We have traced the entire process: the notification gets received with all the info properly (the url is also correct, actually if he clicks right when the message is received it works just fine). But if the notification lives there for a while it gets 'emptied'.

The message being sent is pretty simple (just for showing there are no TTLs, nor expiration parameters being used). The curl command looks like that:

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"

This is the code for processing the push message on the service worker:

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);
    }
}

And the code for managing notificationclick event is pretty straightforward:

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));
});

Is there any reason for loosing the payload after a certain time on the service worker context? Is this time specified on any documentation somewhere? Thanks in advance for your help!

like image 515
nnimis Avatar asked Jul 20 '18 18:07

nnimis


Video Answer


1 Answers

//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);
        }
    }());
});

so in this basically, on click of notification you are redirected to application ,and if the application is not open you are opening the application in new tab , as you are concerned that in event you are not able to get the click_action, could you try event.notification.data.FCM_MSG.notification.click_action by using this and see if you are getting the url, and add the event listener in beginning of service worker https://github.com/firebase/quickstart-js/issues/102

like image 189
ravi Avatar answered Sep 22 '22 06:09

ravi