Skip to content

Make sure notification permissions are granted before calling getToken #2489

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

Merged
merged 2 commits into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
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
18 changes: 9 additions & 9 deletions packages/functions/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,16 @@ export class ContextProvider {
}

async getInstanceIdToken(): Promise<string | undefined> {
try {
if (!this.messaging) {
return undefined;
}
if (
!this.messaging ||
!('Notification' in self) ||
Notification.permission !== 'granted'
) {
return undefined;
}

const token = await this.messaging.getToken();
if (!token) {
return undefined;
}
return token;
try {
return this.messaging.getToken();
} catch (e) {
// We don't warn on this, because it usually means messaging isn't set up.
// console.warn('Failed to retrieve instance id token.', e);
Expand Down
1 change: 1 addition & 0 deletions packages/functions/test/browser/callable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ describe('Firebase Functions > Call', () => {

// Stub out the messaging method get an instance id token.
const stub = sinon.stub(messagingMock, 'getToken').callThrough();
sinon.stub(Notification, 'permission').value('granted');

const func = functions.httpsCallable('instanceIdTest');
const result = await func({});
Expand Down