Hi everyone I really need some help. I’ve looked everywhere but couldn’t find an answer.
I have a PWA with push notifications and badging.
- the PWA works fine in iOS: it receives notifications AND setAppBadge works fine even if the app is closed (i.e. the badge is correctly set)
- on macOS Sonoma 14.4.1 with the PWA/ web app installed to dock via Safari 17.4.1: everything works fine it the app is open; if the app is closed however I still get the push notification alert but setAppBadge doesn’t seem to work (i.e. the badge is not set)
Here’s the “test” code from the service worker:
<code>self.addEventListener('push', (event) => {
let promises = [];
if ('setAppBadge' in self.navigator) {
const badgeCount = 1; //fake data
const promise = self.navigator.setAppBadge(badgeCount);
promises.push(promise);
}
promises.push(self.registration.showNotification("You've got mail!"));
event.waitUntil(Promise.all(promises));
});
</code>
<code>self.addEventListener('push', (event) => {
let promises = [];
if ('setAppBadge' in self.navigator) {
const badgeCount = 1; //fake data
const promise = self.navigator.setAppBadge(badgeCount);
promises.push(promise);
}
promises.push(self.registration.showNotification("You've got mail!"));
event.waitUntil(Promise.all(promises));
});
</code>
self.addEventListener('push', (event) => {
let promises = [];
if ('setAppBadge' in self.navigator) {
const badgeCount = 1; //fake data
const promise = self.navigator.setAppBadge(badgeCount);
promises.push(promise);
}
promises.push(self.registration.showNotification("You've got mail!"));
event.waitUntil(Promise.all(promises));
});
The code is straight out from https://webkit.org/blog/14112/badging-for-home-screen-web-apps/
I tried other combinations/ permutations (number-string arg, show notification before-after setappbadge, …). It always works fine on iOS, not on macOS
To summarize: setAppBadge works as expected with the PWA/ webapp closed on an iPhone, not on a Mac
Thanks in advance