Reputation: 193
I am using firebase messaging for web push notification with react. But browser show this message
Messaging: This browser doesn't support the API's required to use the firebase SDK. (messaging/unsupported-browser)
This is code :
const initializedFirebaseApp = firebase.initializeApp({
apiKey: "XXXXXX",
authDomain: "XXXXXXX",
databaseURL: "XXXXXXXXX",
projectId: "XXXXXX",
storageBucket: "XXXX",
messagingSenderId: "XXXXXX",
appId: "XXXXXX"
});
if (firebase.messaging.isSupported()) {
let messaging = initializedFirebaseApp.messaging();
}
firebase.messaging.isSupported()
is always returning the false. Is there any way I should proceed?
Version for react : 16.8.2 and firebase version : 6.0.2
Upvotes: 15
Views: 38018
Reputation: 618
I used dynamic imports for solving this issue so that the file is not even evaluated before the feature is detected. I am using firebase SDK 8.2.0. This is how my useEffect function looks like on the top level of my app.
import { isSupported } from "firebase/messaging";
useEffect(() => {
(async () => {
const hasFirebaseMessagingSupport = await isSupported();
if (hasFirebaseMessagingSupport) {
const { requestForToken } = await import("./api/cloud-notification/firebase");
await requestForToken();
}
})();
}, []);
This is how my firebase connection file looks like ("./api/cloud-notification/firebase"):
import { initializeApp } from "firebase/app";
import { getMessaging, getToken } from "firebase/messaging";
const FIREBASE_VAPID_KEY = "your-firebase-public-vapid-key";
const firebaseConfig = {
apiKey: 'api-key',
authDomain: 'project-id.firebaseapp.com',
projectId: 'project-id',
storageBucket: 'project-id.appspot.com',
messagingSenderId: 'sender-id',
appId: 'app-id',
measurementId: 'G-measurement-id',
};
initializeApp(firebaseConfig);
const messaging = getMessaging();
const getFirebaseToken = async () => {
try {
const currentToken = await getToken(messaging, { vapidKey: FIREBASE_VAPID_KEY });
if (!currentToken) {
console.log("No registration token available. Request permission to generate one.");
}
} catch (error) {
console.log("An error occurred while retrieving token. ", error);
}
};
export const requestForToken = async () => {
try {
const permission = await Notification.requestPermission();
if (permission === "granted") {
await getFirebaseToken();
}
} catch (error) {
console.log("An error occurred while getting user permission. ", error);
}
};
Upvotes: 2
Reputation: 61
isSupported() in version 9 return Promise so you should wait for resolving
const messaging = (async () => {
try {
const isSupportedBrowser = await isSupported();
if (isSupportedBrowser) {
return getMessaging(config);
}
console.log('Firebase not supported this browser');
return null;
} catch (err) {
console.log(err);
return null;
}
})();
If you are using version 9 you should pass messaging to (onMessageListener resolver and getToken )
onMessageListener
export const onMessageListener = async () =>
new Promise((resolve) =>
(async () => {
const messagingResolve = await messaging;
onMessage(messagingResolve, (payload) => {
// console.log('On message: ', messaging, payload);
resolve(payload);
});
})()
);
getToken
export const requestForToken = async (dispatch) => {
try {
const messagingResolve = await messaging;
const currentToken = await getToken(messagingResolve, {
vapidKey: *your FCM APP SERVER KEY*,
});
if (currentToken) {
*your code*
}
} catch (err) {
console.log('An error occurred while retrieving token. ', err);
}
};
Upvotes: 6
Reputation: 703
In addition to the above explanation you can check if the browser supports messaging by doing:
const messaging = firebase.messaging.isSupported() ? firebase.messaging() : null
Upvotes: 16
Reputation: 366
FCM supports only in localhost and the https enabled sites only. if you want FCM to support you need to either work on localhost or deploy somewhere (you can use firebase).
If you are using proxy using nginx like local.somehost.com cloud messaging doesn't support. To solve this you need to make your local.somehost.com as HTTPS you can install openssl and certificate and key in your nginx.
I think this solves your problem.
Upvotes: 23