I was able to generate the fcm token flawlessly 2 months ago but I’m now facing errors. No connection to push daemon is the error I’m facing. Trying in Safari using both iOS user agent (16.4) and Safari 16.6 as well as an iPhone 14 iOS 16. I’ve tried on https & localhost. I was able to generate the token without a user gesture in the past but I have changed my code to wait for a user gesture.
Chrome and Android devices work fine.
Here is the code which generates the token everywhere except iOS
import app from "./init-firebase.js";
import { getMessaging, getToken } from "https://www.gstatic.com/firebasejs/10.5.2/firebase-messaging.js";
const messaging = getMessaging(app);
async function getNotified() {
var notificationBody = {}
!("Notification" in window)
? alert("This browser does not support desktop notification")
:
// Checks backend & if user is subscribed a 202 is sent back
await fetch('/get-check-if-subscribed', {
method: "GET",
headers: {
"Content-Type": "application/json"
}
}).then((data) => {
if (data.status == 202) {
Notification.requestPermission().then(() => {
if (Notification.permission == 'granted') {
getToken(messaging, { vapidKey: "*****" })
.then((currentToken) => {
notificationBody = {
fcm_token: currentToken
}
fetch("/create-subscription", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(notificationBody)
})
.then(() => {
return window.navigator.serviceWorker
.getRegistration('/firebase-cloud-messaging-push-scope')
.then((serviceWorker) => {
if (serviceWorker) return serviceWorker;
return window.navigator.serviceWorker.register('../firebase-messaging-sw.js', {
scope: '/firebase-cloud-messaging-push-scope',
});
});
}).catch((e) => /* error happens in this block */ alert(e))
})
}
}).catch((err) => alert(err))
}
})
}
export { getNotified };