@@ -31,17 +31,17 @@ export function registerMessaging(instance: _FirebaseNamespace): void {
3131 const factoryMethod : FirebaseServiceFactory = app => {
3232 if ( self && 'ServiceWorkerGlobalScope' in self ) {
3333 // Running in ServiceWorker context
34- return new SWController ( app ) ;
34+ if ( isSWControllerSupported ( ) ) {
35+ return new SWController ( app ) ;
36+ }
3537 } else {
3638 // Assume we are in the window context.
37-
38- // Check that the required API's are available
39- if ( ! isWindowControllerSupported ( ) ) {
40- throw errorFactory . create ( ERROR_CODES . UNSUPPORTED_BROWSER ) ;
39+ if ( isWindowControllerSupported ( ) ) {
40+ return new WindowController ( app ) ;
4141 }
42-
43- return new WindowController ( app ) ;
4442 }
43+
44+ throw errorFactory . create ( ERROR_CODES . UNSUPPORTED_BROWSER ) ;
4545 } ;
4646
4747 const namespaceExports = {
@@ -74,7 +74,7 @@ declare module '@firebase/app-types' {
7474}
7575
7676/**
77- * Checks to see if the required API's are valid or not .
77+ * Checks to see if the required APIs exist .
7878 */
7979function isWindowControllerSupported ( ) : boolean {
8080 return (
@@ -86,3 +86,15 @@ function isWindowControllerSupported(): boolean {
8686 PushSubscription . prototype . hasOwnProperty ( 'getKey' )
8787 ) ;
8888}
89+
90+ /**
91+ * Checks to see if the required APIs exist within SW Context.
92+ */
93+ function isSWControllerSupported ( ) : boolean {
94+ return (
95+ 'PushManager' in self &&
96+ 'Notification' in self &&
97+ ServiceWorkerRegistration . prototype . hasOwnProperty ( 'showNotification' ) &&
98+ PushSubscription . prototype . hasOwnProperty ( 'getKey' )
99+ ) ;
100+ }
0 commit comments