Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions packages-exp/messaging-compat/src/messaging-compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import {
MessagePayload,
deleteToken,
getToken,
onMessage,
onBackgroundMessage
onBackgroundMessage,
onMessage
} from '@firebase/messaging-exp';
import { NextFn, Observer, Unsubscribe } from '@firebase/util';

Expand All @@ -46,6 +46,47 @@ export interface MessagingCompat {
): Unsubscribe;
}

export function isSupported(): boolean {
if (self && 'ServiceWorkerGlobalScope' in self) {
// Running in ServiceWorker context
return isSwSupported();
} else {
// Assume we are in the window context.
return isWindowSupported();
}
}

/**
* Checks to see if the required APIs exist.
*/
function isWindowSupported(): boolean {
return (
'indexedDB' in window &&
indexedDB !== null &&
navigator.cookieEnabled &&
'serviceWorker' in navigator &&
'PushManager' in window &&
'Notification' in window &&
'fetch' in window &&
ServiceWorkerRegistration.prototype.hasOwnProperty('showNotification') &&
PushSubscription.prototype.hasOwnProperty('getKey')
);
}

/**
* Checks to see if the required APIs exist within SW Context.
*/
function isSwSupported(): boolean {
return (
'indexedDB' in self &&
indexedDB !== null &&
'PushManager' in self &&
'Notification' in self &&
ServiceWorkerRegistration.prototype.hasOwnProperty('showNotification') &&
PushSubscription.prototype.hasOwnProperty('getKey')
);
}

export class MessagingCompatImpl implements MessagingCompat, _FirebaseService {
swRegistration?: ServiceWorkerRegistration;
vapidKey?: string;
Expand Down