Skip to content

Commit

Permalink
fix(local-notifications): Adding check for new Notification support (
Browse files Browse the repository at this point in the history
…#295)

* Adding check to see if `new Notification` is supported

* Adding additional check to prevent test from sending empty notification on supported browsers

* switching to `('Notification' in window)` for consistency

* Renaming `checkNotificationSupport` to `hasNotificationSupport`

Co-authored-by: jcesarmobile <jcesarmobile@gmail.com>
  • Loading branch information
theproducer and jcesarmobile authored Mar 9, 2021
1 parent 72be326 commit a806f22
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions local-notifications/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class LocalNotificationsWeb
}

async schedule(options: ScheduleOptions): Promise<ScheduleResult> {
if (!('Notification' in window)) {
if (!this.hasNotificationSupport()) {
throw this.unavailable('Notifications not supported in this browser.');
}

Expand Down Expand Up @@ -71,7 +71,7 @@ export class LocalNotificationsWeb
}

async requestPermissions(): Promise<PermissionStatus> {
if (!('Notification' in window)) {
if (!this.hasNotificationSupport()) {
throw this.unavailable('Notifications not supported in this browser.');
}

Expand All @@ -83,7 +83,7 @@ export class LocalNotificationsWeb
}

async checkPermissions(): Promise<PermissionStatus> {
if (!('Notification' in window)) {
if (!this.hasNotificationSupport()) {
throw this.unavailable('Notifications not supported in this browser.');
}

Expand All @@ -94,6 +94,26 @@ export class LocalNotificationsWeb
return { display };
}

protected hasNotificationSupport = (): boolean => {
if (!('Notification' in window) || !Notification.requestPermission) {
return false;
}

if (Notification.permission !== 'granted') {
// don't test for `new Notification` if permission has already been granted
// otherwise this sends a real notification on supported browsers
try {
new Notification('');
} catch (e) {
if (e.name == 'TypeError') {
return false;
}
}
}

return true;
};

protected transformNotificationPermission(
permission: NotificationPermission,
): PermissionState {
Expand Down

0 comments on commit a806f22

Please sign in to comment.