我正在尝试使用以下代码获取FCM令牌
initializePush() {
const messaging = firebase.messaging();
messaging
.requestPermission()
.then(() => {
return messaging.getToken();
})
.then(token => {
console.log("FCM Token:", token);
//Update FCM Token in DB
})
.catch(error => {
if (error.code === "messaging/permission-blocked") {
console.log("Please Unblock Notification Request Manually");
} else {
console.log("Error Occurred", error);
}
});
messaging.onMessage(payload => {
console.log("Notification Received", payload);
//this is the function that gets triggered when you receive a
//push notification while you’re on the page. So you can
//create a corresponding UI for you to have the push
//notification handled.
});
}
如果我在本地主机上运行应用程序,则此代码有效并返回FCM令牌,但 当我托管应用程序时,它将返回以下错误。
FirebaseError {code:“ messaging / unsupported-browser”,消息: “消息传递:此浏览器不支持API的... firebase SDK。 (消息/不支持的浏览器)。”,堆栈:“ FirebaseError:消息传递: 该浏览器没有 sup…4:8081 / vendor.52afd7daf6e00ece549d.js:128116:307)“}
任何线索为何在托管应用程序上会发生这种情况?