-
Notifications
You must be signed in to change notification settings - Fork 943
Request notification permission in getToken if permission is "default" #2421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,14 +70,16 @@ export abstract class BaseController | |
this.tokenDetailsModel = new TokenDetailsModel(services); | ||
} | ||
|
||
async getToken(): Promise<string | null> { | ||
// Check with permissions | ||
const currentPermission = this.getNotificationPermission_(); | ||
if (currentPermission === 'denied') { | ||
async getToken(): Promise<string> { | ||
// Check notification permission. | ||
let permission = this.getNotificationPermission(); | ||
if (permission === 'default') { | ||
// The user hasn't allowed or denied notifications yet. Ask them. | ||
permission = await this.requestNotificationPermission(); | ||
} | ||
|
||
if (permission !== 'granted') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't it be a breaking change for users who have special handling for Perhaps a safer option is keep returning There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about that too, but I'm pretty sure that like 95% of our users just copy paste what we do in quickstart or our guides, which is requesting permission and calling Besides, previously So if a developer were handling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense , but wouldn't this change make us request permission for a second time if the developer followed the quickstart where they call I don't know how I feel about releasing it without a major version bump. FCM is one of the most popular SDKs, so it could affect a lot of people. What is the motivation for this change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discussed offline, this change would not affect apps that followed the quickstart, because they won't call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, this change affects functions.httpsCallable. Since it aquires FCM token automatically, when calling some function by httpsCallable, the browser asks user for notification permission. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've just ran into this issue pointed out by @VagnerGon, I had an httpsCallable which doesn't require a user to be logged in, but on executing the function it auto pops up the notification permission on page load which is really not best practice. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I didn't realize Functions was doing that, sorry about that. This was fixed in #2489. |
||
throw errorFactory.create(ErrorCode.NOTIFICATIONS_BLOCKED); | ||
} else if (currentPermission !== 'granted') { | ||
// We must wait for permission to be granted | ||
return null; | ||
} | ||
|
||
const swReg = await this.getSWRegistration_(); | ||
|
@@ -327,10 +329,25 @@ export abstract class BaseController | |
/** | ||
* Returns the current Notification Permission state. | ||
*/ | ||
getNotificationPermission_(): NotificationPermission { | ||
private getNotificationPermission(): NotificationPermission { | ||
return Notification.permission; | ||
} | ||
|
||
/** | ||
* Requests notification permission from the user. | ||
*/ | ||
private async requestNotificationPermission(): Promise< | ||
NotificationPermission | ||
> { | ||
if (!Notification.requestPermission) { | ||
// Notification.requestPermission() is not available in service workers. | ||
// Return the current permission. | ||
return Notification.permission; | ||
} | ||
|
||
return Notification.requestPermission(); | ||
} | ||
|
||
getTokenDetailsModel(): TokenDetailsModel { | ||
return this.tokenDetailsModel; | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.