Skip to content

Commit 5cc3b96

Browse files
committed
Add listeners registration to messaging-compat
1 parent 9776fc4 commit 5cc3b96

File tree

4 files changed

+44
-13
lines changed

4 files changed

+44
-13
lines changed

common/api-review/messaging-exp.api.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ export function onBackgroundMessage(messaging: FirebaseMessaging, nextOrObserver
6767
// @public
6868
export function onMessage(messaging: FirebaseMessaging, nextOrObserver: NextFn<MessagePayload> | Observer<MessagePayload>): Unsubscribe;
6969

70+
// @internal
71+
export function _registerListeners(messaging: FirebaseMessaging): void;
72+
7073
export { Unsubscribe }
7174

7275

packages-exp/messaging-compat/src/messaging-compat.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
import {
2323
FirebaseMessaging,
2424
MessagePayload,
25+
_registerListeners,
2526
deleteToken,
2627
getToken,
2728
onBackgroundMessage,
@@ -88,22 +89,11 @@ function isSwSupported(): boolean {
8889
}
8990

9091
export class MessagingCompatImpl implements MessagingCompat, _FirebaseService {
91-
swRegistration?: ServiceWorkerRegistration;
92-
vapidKey?: string;
93-
94-
onBackgroundMessageHandler:
95-
| NextFn<MessagePayload>
96-
| Observer<MessagePayload>
97-
| null = null;
98-
99-
onMessageHandler:
100-
| NextFn<MessagePayload>
101-
| Observer<MessagePayload>
102-
| null = null;
103-
10492
constructor(readonly app: AppCompat, readonly _delegate: FirebaseMessaging) {
10593
this.app = app;
10694
this._delegate = _delegate;
95+
96+
_registerListeners(_delegate);
10797
}
10898

10999
async getToken(options?: {

packages-exp/messaging-exp/src/helpers/register.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,17 @@ import {
2323
} from '@firebase/component';
2424
import { ERROR_FACTORY, ErrorCode } from '../util/errors';
2525
import { isSwSupported, isWindowSupported } from '../api/isSupported';
26+
import {
27+
onNotificationClick,
28+
onPush,
29+
onSubChange
30+
} from '../listeners/sw-listeners';
2631

32+
import { FirebaseMessaging } from '../interfaces/public-types';
2733
import { MessagingService } from '../messaging-service';
34+
import { ServiceWorkerGlobalScope } from '../util/sw-types';
2835
import { _registerComponent } from '@firebase/app-exp';
36+
import { messageEventListener } from '../listeners/window-listener';
2937

3038
const WindowMessagingFactory: InstanceFactory<'messaging-exp'> = (
3139
container: ComponentContainer
@@ -86,3 +94,32 @@ export function registerMessagingInSw(): void {
8694
new Component('messaging-exp', SwMessagingFactory, ComponentType.PUBLIC)
8795
);
8896
}
97+
98+
declare const self: ServiceWorkerGlobalScope;
99+
/**
100+
* Conditionally registers the listeners. Theses registrations are done in `getMessagingInSw` and
101+
* `getMessagingInWindow` for the v9 SDK. This method exists for `messaging-compat` because the
102+
* injected messaging instance to `messaging-compat` isn't created through the `getMessaging`
103+
* method. Thus the main package of v9 needs to export this method to support `messaging-compat`.
104+
*
105+
* @internal
106+
*/
107+
export function _registerListeners(messaging: FirebaseMessaging): void {
108+
if (!!navigator) {
109+
// in window
110+
navigator.serviceWorker.addEventListener('message', e =>
111+
messageEventListener(messaging as MessagingService, e)
112+
);
113+
} else {
114+
// in sw
115+
self.addEventListener('push', e => {
116+
e.waitUntil(onPush(e, messaging as MessagingService));
117+
});
118+
self.addEventListener('pushsubscriptionchange', e => {
119+
e.waitUntil(onSubChange(e, messaging as MessagingService));
120+
});
121+
self.addEventListener('notificationclick', e => {
122+
e.waitUntil(onNotificationClick(e));
123+
});
124+
}
125+
}

packages-exp/messaging-exp/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export {
3333
getMessagingInWindow as getMessaging,
3434
onBackgroundMessage
3535
} from './api';
36+
export { _registerListeners } from './helpers/register';
3637
export { isWindowSupported as isSupported } from './api/isSupported';
3738
export * from './interfaces/public-types';
3839

0 commit comments

Comments
 (0)