Skip to content

Commit 71b7abf

Browse files
authored
Fix FCM in Chrome Extensions (#1597)
Prevents sending messages to Chrome Extension background pages, which are always considered visible in Chrome. That causes our SDK try to send the message to the background page and to not show the notification. I'll also investigate if background page being considered a visible client is a bug in Chrome itself, and try to get it fixed if I can. But for now this seems like a good workaround.
1 parent 0c9badf commit 71b7abf

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

packages/messaging/src/controllers/sw-controller.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,11 @@ export class SwController extends BaseController {
333333
const clientList = await getClientList();
334334

335335
return clientList.some(
336-
(client: WindowClient) => client.visibilityState === 'visible'
336+
(client: WindowClient) =>
337+
client.visibilityState === 'visible' &&
338+
// Ignore chrome-extension clients as that matches the background pages
339+
// of extensions, which are always considered visible.
340+
!client.url.startsWith('chrome-extension://')
337341
);
338342
}
339343

packages/messaging/test/sw-controller.test.ts

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -400,13 +400,16 @@ describe('Firebase Messaging > *SwController', () => {
400400

401401
return [
402402
{
403-
visibilityState: 'hidden'
403+
visibilityState: 'hidden',
404+
url: 'https://example.com'
404405
},
405406
{
406-
visibilityState: 'prerender'
407+
visibilityState: 'prerender',
408+
url: 'https://firebase.com'
407409
},
408410
{
409-
visibilityState: 'unloaded'
411+
visibilityState: 'unloaded',
412+
url: 'https://google.com'
410413
}
411414
];
412415
}
@@ -428,16 +431,20 @@ describe('Firebase Messaging > *SwController', () => {
428431

429432
return [
430433
{
431-
visibilityState: 'hidden'
434+
visibilityState: 'hidden',
435+
url: 'https://example.com'
432436
},
433437
{
434-
visibilityState: 'prerender'
438+
visibilityState: 'prerender',
439+
url: 'https://firebase.com'
435440
},
436441
{
437-
visibilityState: 'unloaded'
442+
visibilityState: 'unloaded',
443+
url: 'https://google.com'
438444
},
439445
{
440-
visibilityState: 'visible'
446+
visibilityState: 'visible',
447+
url: 'https://mozilla.org'
441448
}
442449
];
443450
}
@@ -448,6 +455,29 @@ describe('Firebase Messaging > *SwController', () => {
448455
const result = await swController.hasVisibleClients_();
449456
expect(result).to.equal(true);
450457
});
458+
459+
it('should return false when a chrome-extension client is visible', async () => {
460+
const clients = {
461+
matchAll: async (options: Partial<ClientQueryOptions>) => {
462+
expect(options).to.deep.equal({
463+
type: 'window',
464+
includeUncontrolled: true
465+
});
466+
467+
return [
468+
{
469+
visibilityState: 'visible',
470+
url: 'chrome-extension://example.com'
471+
}
472+
];
473+
}
474+
};
475+
sandbox.stub(self, 'clients').value(clients);
476+
477+
const swController = new SwController(app);
478+
const result = await swController.hasVisibleClients_();
479+
expect(result).to.equal(false);
480+
});
451481
});
452482

453483
describe('getWindowClient_', () => {

0 commit comments

Comments
 (0)