Skip to content

Commit

Permalink
Merge pull request #1209 from OneSignal/workaround/macOS-Chrome-extra…
Browse files Browse the repository at this point in the history
…-notification

Chrome macOS 15 extra notification workaround
  • Loading branch information
jkasten2 authored Nov 19, 2024
2 parents 1d15945 + c3a3b09 commit 6d3d603
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
53 changes: 53 additions & 0 deletions __test__/unit/sw/serviceWorker.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { ServiceWorker } from '../../../src/sw/serviceWorker/ServiceWorker';

// suppress all internal logging
jest.mock('../../../src/shared/libraries/Log');

function chromeUserAgentDataBrands(): Array<{
brand: string;
version: string;
}> {
return [
{ brand: 'Google Chrome', version: '129' },
{ brand: 'Not=A?Brand', version: '8' },
{ brand: 'Chromium', version: '129' },
];
}

describe('ServiceWorker', () => {
describe('requiresMacOS15ChromiumAfterDisplayWorkaround', () => {
test('navigator.userAgentData undefined', async () => {
delete (navigator as any).userAgentData;
expect(
ServiceWorker.requiresMacOS15ChromiumAfterDisplayWorkaround(),
).toBe(false);
});
test('navigator.userAgentData null', async () => {
(navigator as any).userAgentData = null;
expect(
ServiceWorker.requiresMacOS15ChromiumAfterDisplayWorkaround(),
).toBe(false);
});

test('navigator.userAgentData Chrome on Windows Desktop', async () => {
(navigator as any).userAgentData = {
mobile: false,
platform: 'Windows',
brands: chromeUserAgentDataBrands(),
};
expect(
ServiceWorker.requiresMacOS15ChromiumAfterDisplayWorkaround(),
).toBe(false);
});
test('navigator.userAgentData Chrome on macOS', async () => {
(navigator as any).userAgentData = {
mobile: false,
platform: 'macOS',
brands: chromeUserAgentDataBrands(),
};
expect(
ServiceWorker.requiresMacOS15ChromiumAfterDisplayWorkaround(),
).toBe(true);
});
});
});
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,
);

if (this.requiresMacOS15ChromiumAfterDisplayWorkaround()) {
await awaitableTimeout(1_000);
}
}

// Workaround: For Chromium browsers displaying an extra notification, even
// when background rules are followed.
// For reference, the notification body is "This site has been updated in the background".
// https://issues.chromium.org/issues/378103918
static requiresMacOS15ChromiumAfterDisplayWorkaround(): boolean {
const userAgentData = (navigator as any).userAgentData;
const isMacOS = userAgentData?.platform === 'macOS';
const isChromium = !!userAgentData?.brands?.some(
(item: { brand: string }) => item.brand === 'Chromium',
);
return isMacOS && isChromium;
}

/**
Expand Down

0 comments on commit 6d3d603

Please sign in to comment.