Skip to content

Commit fc04353

Browse files
authored
Add isSupported for messaging-compat (#4829)
1 parent d442eb2 commit fc04353

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

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

+43-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import {
2424
MessagePayload,
2525
deleteToken,
2626
getToken,
27-
onMessage,
28-
onBackgroundMessage
27+
onBackgroundMessage,
28+
onMessage
2929
} from '@firebase/messaging-exp';
3030
import { NextFn, Observer, Unsubscribe } from '@firebase/util';
3131

@@ -46,6 +46,47 @@ export interface MessagingCompat {
4646
): Unsubscribe;
4747
}
4848

49+
export function isSupported(): boolean {
50+
if (self && 'ServiceWorkerGlobalScope' in self) {
51+
// Running in ServiceWorker context
52+
return isSwSupported();
53+
} else {
54+
// Assume we are in the window context.
55+
return isWindowSupported();
56+
}
57+
}
58+
59+
/**
60+
* Checks to see if the required APIs exist.
61+
*/
62+
function isWindowSupported(): boolean {
63+
return (
64+
'indexedDB' in window &&
65+
indexedDB !== null &&
66+
navigator.cookieEnabled &&
67+
'serviceWorker' in navigator &&
68+
'PushManager' in window &&
69+
'Notification' in window &&
70+
'fetch' in window &&
71+
ServiceWorkerRegistration.prototype.hasOwnProperty('showNotification') &&
72+
PushSubscription.prototype.hasOwnProperty('getKey')
73+
);
74+
}
75+
76+
/**
77+
* Checks to see if the required APIs exist within SW Context.
78+
*/
79+
function isSwSupported(): boolean {
80+
return (
81+
'indexedDB' in self &&
82+
indexedDB !== null &&
83+
'PushManager' in self &&
84+
'Notification' in self &&
85+
ServiceWorkerRegistration.prototype.hasOwnProperty('showNotification') &&
86+
PushSubscription.prototype.hasOwnProperty('getKey')
87+
);
88+
}
89+
4990
export class MessagingCompatImpl implements MessagingCompat, _FirebaseService {
5091
swRegistration?: ServiceWorkerRegistration;
5192
vapidKey?: string;

0 commit comments

Comments
 (0)