AudreyHal / React-Firebase-Cloud-Messaging-Demo

Demo of https://dev.to/audreyhal/push-notifications-with-firebase-in-reactjs-1f9i
27 stars 12 forks source link

Getting token twice #1

Open adelpro opened 2 years ago

adelpro commented 2 years ago

suggested solution: get the token with asynch with ".then()" code: const vapidKey = "BDEASiE-8W4AwXjKPVUGqPf93FCAFophD8F2l1-cfJRgJyQjCaprz0NVVUnzSrM9QRYuKgrX_dTt6jBcfktR-U8"; export const requestForToken = () => { getToken(messaging, { vapidKey }).then((currentToken) => { if (currentToken) { //do anything with token console.log("current token for client: ", currentToken); } else { // Show permission request UI console.log( "No registration token available. Request permission to generate one." ); } }); };

sohan-gts commented 8 months ago

is it going to work well with latest firebase version "firebase": "^10.6.0",

in my project its not working

this error is coming and also this react is created with vite in ts .

Catch og getToken FirebaseError: Messaging: We are unable to register the default service worker. Failed to register a ServiceWorker for scope

sohan-gts commented 8 months ago

suggested solution: get the token with asynch with ".then()" code: const vapidKey = "BDEASiE-8W4AwXjKPVUGqPf93FCAFophD8F2l1-cfJRgJyQjCaprz0NVVUnzSrM9QRYuKgrX_dTt6jBcfktR-U8"; export const requestForToken = () => { getToken(messaging, { vapidKey }).then((currentToken) => { if (currentToken) { //do anything with token console.log("current token for client: ", currentToken); } else { // Show permission request UI console.log( "No registration token available. Request permission to generate one." ); } }); };

@adelpro it is due to react strictmode 2 times rendering