@@ -23,9 +23,16 @@ import {
23
23
} from '@firebase/component' ;
24
24
import { ERROR_FACTORY , ErrorCode } from '../util/errors' ;
25
25
import { isSwSupported , isWindowSupported } from '../api/isSupported' ;
26
+ import {
27
+ onNotificationClick ,
28
+ onPush ,
29
+ onSubChange
30
+ } from '../listeners/sw-listeners' ;
26
31
27
32
import { MessagingService } from '../messaging-service' ;
33
+ import { ServiceWorkerGlobalScope } from '../util/sw-types' ;
28
34
import { _registerComponent } from '@firebase/app-exp' ;
35
+ import { messageEventListener } from '../listeners/window-listener' ;
29
36
30
37
const WindowMessagingFactory : InstanceFactory < 'messaging-exp' > = (
31
38
container : ComponentContainer
@@ -44,13 +51,20 @@ const WindowMessagingFactory: InstanceFactory<'messaging-exp'> = (
44
51
throw ERROR_FACTORY . create ( ErrorCode . INDEXED_DB_UNSUPPORTED ) ;
45
52
} ) ;
46
53
47
- return new MessagingService (
54
+ const messaging = new MessagingService (
48
55
container . getProvider ( 'app-exp' ) . getImmediate ( ) ,
49
56
container . getProvider ( 'installations-exp-internal' ) . getImmediate ( ) ,
50
57
container . getProvider ( 'analytics-internal' )
51
58
) ;
59
+
60
+ navigator . serviceWorker . addEventListener ( 'message' , e =>
61
+ messageEventListener ( messaging as MessagingService , e )
62
+ ) ;
63
+
64
+ return messaging ;
52
65
} ;
53
66
67
+ declare const self : ServiceWorkerGlobalScope ;
54
68
const SwMessagingFactory : InstanceFactory < 'messaging-exp' > = (
55
69
container : ComponentContainer
56
70
) => {
@@ -68,11 +82,23 @@ const SwMessagingFactory: InstanceFactory<'messaging-exp'> = (
68
82
throw ERROR_FACTORY . create ( ErrorCode . INDEXED_DB_UNSUPPORTED ) ;
69
83
} ) ;
70
84
71
- return new MessagingService (
85
+ const messaging = new MessagingService (
72
86
container . getProvider ( 'app-exp' ) . getImmediate ( ) ,
73
87
container . getProvider ( 'installations-exp-internal' ) . getImmediate ( ) ,
74
88
container . getProvider ( 'analytics-internal' )
75
89
) ;
90
+
91
+ self . addEventListener ( 'push' , e => {
92
+ e . waitUntil ( onPush ( e , messaging as MessagingService ) ) ;
93
+ } ) ;
94
+ self . addEventListener ( 'pushsubscriptionchange' , e => {
95
+ e . waitUntil ( onSubChange ( e , messaging as MessagingService ) ) ;
96
+ } ) ;
97
+ self . addEventListener ( 'notificationclick' , e => {
98
+ e . waitUntil ( onNotificationClick ( e ) ) ;
99
+ } ) ;
100
+
101
+ return messaging ;
76
102
} ;
77
103
78
104
export function registerMessagingInWindow ( ) : void {
@@ -81,8 +107,13 @@ export function registerMessagingInWindow(): void {
81
107
) ;
82
108
}
83
109
110
+ /**
111
+ * The messaging instance registered in sw is named differently than that of in client. This is
112
+ * because both `registerMessagingInWindow` and `registerMessagingInSw` would be called in
113
+ * `messaging-compat` and component with the same name can only be registered once.
114
+ */
84
115
export function registerMessagingInSw ( ) : void {
85
116
_registerComponent (
86
- new Component ( 'messaging-exp' , SwMessagingFactory , ComponentType . PUBLIC )
117
+ new Component ( 'messaging-sw- exp' , SwMessagingFactory , ComponentType . PUBLIC )
87
118
) ;
88
119
}
0 commit comments