我正在尝试在 iOS Safari 浏览器中实现 Firebase 云消息传递,因为现在支持推送通知和通知 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);
// ...
});
}
})
}
我从桌面使用功能
requestPermission()
,它成功请求权限。但是,在 iOS Safari 上,即使在浏览器中定义了通知并启用了推送通知和通知 API,Notification.requestPermission()
也不会向用户显示错误或提示。
我通过单击按钮调用了此函数,这可以防止我收到有关“用户手势”的错误。
如何解决这个问题?
我知道为时已晚,但已在您的
"display": "standalone"
文件中添加了 manifest.json
。
另请确保此 <link rel="manifest" href="manifest.json">
代码存在于您的 index.html 文件的 <head></head>
标记内。
执行此操作后,您必须从 Safari 历史记录中清除您网站的所有网站数据(缓存)。然后将您的网站添加到主屏幕。之后应该可以工作。根据this,需要将应用程序添加到主屏幕以进行推送通知。一旦用户在主屏幕上安装您的应用程序,它将收到推送通知。