我正在尝试向我的网站实施推送通知,但我设法在台式机上做到了,但是
没有提示询问用户对移动设备的许可。
我试过在iphone上使用safari,在iphone上使用chrome(甚至不能在那里注册服务工作者),在iphone上使用duckduckgo浏览器,并且没有弹出窗口在桌面上可以正常工作。
我的代码:
async function registerSw () {
if ('serviceWorker' in navigator) {
alert('supported'); // this pops up in safari
navigator.serviceWorker.register('./sw.js').then(function(registration) {
subscribeToPush();
}).catch(error => console.log(error));
} else {
// I get this on iphone version of chrome
alert('Service workers not supported');
}
}
订阅功能:
async function subscribeToPush () {
navigator.serviceWorker.ready.then(function(registration) {
alert('inside subscribe'); // this shows up too
registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: urlB64ToUint8Array('BL9qxdhqL_CM1ROLo6AUfeBvEyUuD7EHT3lAz8ksBZSYPsdE6q__uU2FoX9lr5FtmWtlHs-HRMHen3Ki8WWSVA4')
})
.then(function(subscription) {
alert('sub'); // this doesnt show up
// The subscription was successful
// let key = new Uint8Array(subscription.getKey('p256dh'));
// let auth = new Uint8Array(subscription.getKey('auth'));
let key = btoa(String.fromCharCode.apply(null, new Uint8Array(subscription.getKey('p256dh')))) ;
let auth = btoa(String.fromCharCode.apply(null, new Uint8Array(subscription.getKey('auth')))) ;
let endpoint = subscription.endpoint;
let contentEncoding = (PushManager.supportedContentEncodings || ['aesgcm'])[0];
let data = {
'p256dh': key,
'auth': auth,
'endpoint': endpoint,
'contentEncoding': contentEncoding,
};
alert('Before request')
setTimeout(function() {
ajax(data);
}, 2000);
})
.catch(function(e) {
console.log( alert('permission missing') );
console.log( e );
});
});
}
最佳答案
当前(2019年),iOS上没有浏览器支持推送通知。
此外,桌面版Safari也支持使用称为APNs(而非Push API标准)的专有技术向网站发出通知。