firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

getToken() for Push API, requires Notification API support

Open SteveKennedy opened this issue 10 months ago • 6 comments

Operating System

macOS 14.1

Browser Version

Safari 17.4

Firebase SDK Version

10.11

Firebase SDK Product:

Messaging

Describe your project's tooling

Angular only

Describe the problem

When calling getToken() to get token to use for Web Push API, it responds with error about not having Notification API support. Specifically "ReferenceError: Can't find variable: Notification"

If someone wants to use getToken() only for Web Push API support, and never plans to support Notification API, this seems like a bug?

This issue is also described in: https://stackoverflow.com/questions/78359582/push-api-without-notification-api

Steps and code to reproduce issue

  1. Create a template Angular project.
  2. Add FirebaseJS or even Angular Fire.
  3. Call getToken() on a browser that does not support Notification API.

SteveKennedy avatar Apr 20 '24 20:04 SteveKennedy

Did you try add your web app to the home screen? It seems Safari does not support web push feature but web app added to the home screen (or dock). + I am setting this thread straight. Safari for mac does not need to be added to dock to use Push API. (05/03/2024)

I faced same issue in my React project and resolved like this:

// firebaseConfig.js
const firebaseConfig = {
  // API KEYS
};
const app = initializeApp(firebaseConfig);
const isFirebaseSupported = async () => {
  const supported = await isSupported(); // imported from 'firebase/messaging'
  return supported ? getMessaging(app) : null;
}
export const messaging = await isFirebaseSupported();
// LoginPage.jsx
const handleLogin = () => {
  if ('Notification' in window && Notification.permission !== 'granted') {
    alert('Click allow when dialog shows up');
    const clientToken = await getToken(messaging, { vapidKey });
  }
}

return <Button onClick={handleLogin} />
}

Make sure to put if statement to check if the browser support Notification API(only home screen web apps works in iOS), and don't let getToken automatically run without user gesture.

Hope this helps.

juyeongnoh avatar Apr 21 '24 11:04 juyeongnoh

Safari says it does support Push API (aka "messages") natively, without adding to home screen. You only have to add your app to the home screen if you want Notifications API. I'm really trying my best to avoid Notifications API. I don't want Notification API. I simply only want Push API.

You still need a token to use Push API. And calling firebase 's getToken() for some reason also requires you to have Notification API support. Which feels like a bug.

https://developer.mozilla.org/en-US/docs/Web/API/Push_API

https://developer.mozilla.org/en-US/docs/Web/API/notification

SteveKennedy avatar Apr 21 '24 13:04 SteveKennedy

I think this is why you're getting that error message. getToken() itself uses Notification API internally.

juyeongnoh avatar Apr 21 '24 13:04 juyeongnoh

I think this is why you're getting that error message. getToken() itself uses Notification API internally.

I agree. It references the Notification type, which isn't valid for browsers that don't support notification API.

But if I just need to use getToken() to get a token for Push Message API, then this fails horribly.

I don't want to use Notification API at all.

SteveKennedy avatar Apr 21 '24 13:04 SteveKennedy

Receiving on mac safari shouldn't require app being added to home screen.

Unsure why your browser isn't supporting Notification API. According to browser compatibility, notification api is supported/enabled years before push API. My expectation is that any safari clients that has push api enabled should have notification api enabled as well (unless there is some secret config somewhere)

zwu52 avatar May 02 '24 23:05 zwu52

I'll set my thread straight. Didn't know that Safari for mac does not need to be added to home screen to use push api.

juyeongnoh avatar May 03 '24 01:05 juyeongnoh

Hey @SteveKennedy. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot avatar May 23 '24 01:05 google-oss-bot

Since there haven't been any recent updates here, I am going to close this issue.

@SteveKennedy if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.

google-oss-bot avatar May 30 '24 01:05 google-oss-bot