Ary*_*ach 7 javascript safari ios firebase firebase-cloud-messaging
我正在尝试在 iOS Safari 浏览器中实现 Firebase Cloud Messaging,因为现在支持推送通知和通知 API。
function requestPermission() {
console.log("request permission")
Notification.requestPermission().then((permission) => {
if (permission === 'granted') {
console.log('FIREBASE CLOUD MESSAGING Notification permission granted.');
messaging.getToken(messaging, { vapidKey: '<my-key>' }).then((currentToken) => {
if (currentToken) {
// Send the token to your server and update the UI if necessary
// ...
console.log("FIREBASE CLOUD MESSAGING currentToken", currentToken)
} else {
// Show permission request UI
console.log('FIREBASE CLOUD MESSAGING No registration token available. Request permission to generate one.');
// ...
}
}).catch((err) => {
console.log('FIREBASE CLOUD MESSAGING An error occurred while retrieving token. ', err);
// ...
});
}
})
}
Run Code Online (Sandbox Code Playgroud)
我从桌面使用该功能requestPermission(),它成功请求权限。但是,在 iOS Safari 上,Notification.requestPermission()即使在浏览器中定义了通知并且启用了推送通知和通知 API,也不会向用户显示错误或提示。
我通过单击按钮调用了这个函数,这可以防止我收到有关“用户手势”的错误。
我该如何解决这个问题?