Skip to content
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

getToken() for Push API, requires Notification API support #8193

Open
SteveKennedy opened this issue Apr 20, 2024 · 6 comments
Open

getToken() for Push API, requires Notification API support #8193

SteveKennedy opened this issue Apr 20, 2024 · 6 comments

Comments

@SteveKennedy
Copy link

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 SteveKennedy added new A new issue that hasn't be categoirzed as question, bug or feature request question labels Apr 20, 2024
@juyeongnoh
Copy link

juyeongnoh commented Apr 21, 2024

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.

@SteveKennedy
Copy link
Author

SteveKennedy commented Apr 21, 2024

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

@juyeongnoh
Copy link

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

@SteveKennedy
Copy link
Author

SteveKennedy commented Apr 21, 2024

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.

@jbalidiong jbalidiong added needs-attention and removed new A new issue that hasn't be categoirzed as question, bug or feature request labels Apr 22, 2024
@zwu52
Copy link
Member

zwu52 commented May 2, 2024

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)

@juyeongnoh
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants