Skip to content

Commit

Permalink
Chrome macOS 15 extra notification workaround
Browse files Browse the repository at this point in the history
macOS 15 made some unknown changes that causes Chrome's fragile
notification display detection to fail, which means an extra
notification with the body
"This site has been updated in the background" displays.

This was reported to Google under this issue:
https://issues.chromium.org/issues/378103918

A workaround was found that if you delay ending the service worker
event for a bit then Chrome will detect the notification from macOS.
However this work around won't work 100% of the time since if a
notification is closed very quickly (within the 1000ms). This window
is also much easier to hit if notifications are send back-to-back.
Lastly it is unknown if 1000ms is always enough time, but to much
time isn't good either due what was noted earlier.

We scoped this workaround to the known target of Chromium on macOS 15
so this ~1% chance doesn't cause issues on other platforms. We can't
detect the macOS version so this will effect all versions.

What this does not fix:
There is also an issue where if the OneSignal feature
received receipts / confirmed deliveries is on and the notification is
clicked or closed with in it's wait window this extra notification will
display. We will address this in a different PR.
  • Loading branch information
jkasten2 committed Nov 18, 2024
1 parent 41a141b commit da11637
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/sw/serviceWorker/ServiceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -773,10 +773,27 @@ export class ServiceWorker {
badge: notification.badgeIcon,
};

return self.registration.showNotification(
await self.registration.showNotification(
notification.title,
notificationOptions,
);

await this.afterNotificationDisplayMacOS15ChromiumWorkaround();
}

// Workaround: For Chromium browsers displaying an extra notifiation, even
// when background rules are followed.
// For reference, the notifiation body is "This site has been updated in the background".
// https://issues.chromium.org/issues/378103918
static async afterNotificationDisplayMacOS15ChromiumWorkaround(): Promise<void> {
const userAgentData = (navigator as any).userAgentData;
const isMacOS = userAgentData?.platform === 'macOS';
const isChromium = !!userAgentData?.brands?.some(
(item: { brand: string; }) => item.brand === 'Chromium'
);
if (isMacOS && isChromium) {
await awaitableTimeout(1_000);
}
}

/**
Expand Down

0 comments on commit da11637

Please sign in to comment.