aronduby / hwp

0 stars 1 forks source link

Notification Clicks #152

Open aronduby opened 10 months ago

aronduby commented 10 months ago

Right now all the clicks are opening the app, but some of them are sending URLs that should open instead. Here's code that should be parsed and usable for reference, especially the click handling.

onBackgroundMessage(messaging, (payload) => {
    /*
    {
        "from": "143650738495",
        "messageId": "21dd3d45-5fb8-4a4a-b58c-143d019b1bd7",
        "data": {
            "pinpoint.notification.silentPush": "0",
            "pinpoint.notification.body": "This is a test message",
            "pinpoint.url": "https://google.com",
            "pinpoint.notification.imageUrl": "/public/screenshot-login-1280x720.png",
            "pinpoint.campaign.campaign_id": "_DIRECT",
            "pinpoint.notification.title": "Test Notification",
            "pinpoint.notification.imageIconUrl": "/icons/android-chrome-192x192.png"
        }
    }
    */

    if (payload.data!['pinpoint.notification.silentPush'] !== '0') {
        return;
    }

    const title = payload.data!['pinpoint.notification.title'];
    const options: NotificationOptions = {
        body: payload.data!['pinpoint.notification.body'],
        badge: payload.data?.['pinpoint.notification.smallImageIconUrl'] ?? 'favicon-light.png',
        icon: payload.data?.['pinpoint.notification.imageIconUrl'] ?? '/android-chrome-192x192.png',
        image: payload.data?.['pinpoint.notification.imageUrl'] ?? undefined, 
        tag: payload.messageId,
        data: {
            url: payload.data?.['pinpoint.url'] ?? '/' 
        }
    };    

    self.registration.showNotification(title, options);
});

self.addEventListener('notificationclick', function(event){
    event.notification.close();

    const data = event.notification.data;
    if (!data?.url) {
        return;
    }

    // const url = data.url;
    const url = new URL(data.url, self.location.origin);

    // If the exact url is already open, focus it
    // If the exact doesn't, but there is one with the same origin, then use that one
    // otherwise just open it
    // noinspection FunctionWithInconsistentReturnsJS
    event.waitUntil(
        self.clients.matchAll({ type: "window" })
            .then(function(clientList){
                for (let i=0; i < clientList.length; i++) {
                    const client = clientList[i];
                    const clientUrl = new URL(client.url);

                    if(clientUrl.href === url.href && 'focus' in client) {
                        return client.focus();
                    } else if (clientUrl.origin === url.origin && 'focus' in client && 'navigate' in client) {
                        client.navigate(url);
                        return client.focus();
                    }
                }

                if (self.clients.openWindow) {
                    return self.clients.openWindow(url);
                }
            })
    );

});