I have desktop notifications working just fine in my Progressive Web App (PWA). With the PWA open, when I click the notification, I am able to receive the notification's data property. When the PWA is closed, when I click the notification, the PWA is opened BUT my onclick handler is never executed, nor do I see a way to receive data from the notification on launch of the web app.
How can I receive the notification data property when the PWA is not currently running?
With 2 notifications appearing from my PWA, I want to know which of the PWAs was clicked when my PWA is closed. Looking at the Notification API, I don't see a way to check on startup of my web app.
Here is my code:
if ('Notification' in window) {
const options = {
icon: 'https://i.imgur.com/l8qOen5.png',
image: 'https://i.imgur.com/l8qOen5.png',
requireInteraction: true,
};
if (Notification.permission === 'granted') {
const desktopNotification = new Notification('My Title', {
...options,
body: 'My body text',
data: 'A string I want to receive when PWA is opened',
tag: 'A unique identifier',
});
desktopNotification.onclick = (e) => {
// Not received when PWA is closed and then opened on click of notification
const data = e.currentTarget.data || {};
console.log('desktopNotification clicked!', e, data);
};
}
}