Skip to content

Commit d1f2693

Browse files
authored
Request notification permission in getToken if permission is "d… (#2421)
Instead of returning null and expecting the user to request the notification permission, do it automatically.
1 parent f9d26f7 commit d1f2693

File tree

5 files changed

+376
-357
lines changed

5 files changed

+376
-357
lines changed

packages/messaging-types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
export class FirebaseMessaging {
2828
private constructor();
2929
deleteToken(token: string): Promise<boolean>;
30-
getToken(): Promise<string | null>;
30+
getToken(): Promise<string>;
3131
onMessage(
3232
nextOrObserver: NextFn<any> | Observer<any>,
3333
error?: ErrorFn,

packages/messaging/src/controllers/base-controller.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,16 @@ export abstract class BaseController
7070
this.tokenDetailsModel = new TokenDetailsModel(services);
7171
}
7272

73-
async getToken(): Promise<string | null> {
74-
// Check with permissions
75-
const currentPermission = this.getNotificationPermission_();
76-
if (currentPermission === 'denied') {
73+
async getToken(): Promise<string> {
74+
// Check notification permission.
75+
let permission = this.getNotificationPermission();
76+
if (permission === 'default') {
77+
// The user hasn't allowed or denied notifications yet. Ask them.
78+
permission = await this.requestNotificationPermission();
79+
}
80+
81+
if (permission !== 'granted') {
7782
throw errorFactory.create(ErrorCode.NOTIFICATIONS_BLOCKED);
78-
} else if (currentPermission !== 'granted') {
79-
// We must wait for permission to be granted
80-
return null;
8183
}
8284

8385
const swReg = await this.getSWRegistration_();
@@ -327,10 +329,25 @@ export abstract class BaseController
327329
/**
328330
* Returns the current Notification Permission state.
329331
*/
330-
getNotificationPermission_(): NotificationPermission {
332+
private getNotificationPermission(): NotificationPermission {
331333
return Notification.permission;
332334
}
333335

336+
/**
337+
* Requests notification permission from the user.
338+
*/
339+
private async requestNotificationPermission(): Promise<
340+
NotificationPermission
341+
> {
342+
if (!Notification.requestPermission) {
343+
// Notification.requestPermission() is not available in service workers.
344+
// Return the current permission.
345+
return Notification.permission;
346+
}
347+
348+
return Notification.requestPermission();
349+
}
350+
334351
getTokenDetailsModel(): TokenDetailsModel {
335352
return this.tokenDetailsModel;
336353
}

packages/messaging/src/controllers/window-controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export class WindowController extends BaseController {
7878
* https://developer.mozilla.org/en-US/docs/Web/API/Notification/requestPermission
7979
*/
8080
async requestPermission(): Promise<void> {
81-
if (this.getNotificationPermission_() === 'granted') {
81+
if (Notification.permission === 'granted') {
8282
return;
8383
}
8484

0 commit comments

Comments
 (0)