Javascript pushManager不在移动设备上请求许可
我试图实现推送通知到我的网站,我设法做到这一点在桌面上,但 没有提示要求用户对移动设备进行许可 我试过在iphone上使用safari,在iphone上使用chrome(甚至不能在那里注册服务人员),在iphone上使用duckduckgo浏览器,但在桌面上没有可以正常工作的弹出窗口 我的代码:Javascript pushManager不在移动设备上请求许可,javascript,mobile,push-notification,notifications,web-push,Javascript,Mobile,Push Notification,Notifications,Web Push,我试图实现推送通知到我的网站,我设法做到这一点在桌面上,但 没有提示要求用户对移动设备进行许可 我试过在iphone上使用safari,在iphone上使用chrome(甚至不能在那里注册服务人员),在iphone上使用duckduckgo浏览器,但在桌面上没有可以正常工作的弹出窗口 我的代码: async function registerSw () { if ('serviceWorker' in navigator) { alert('supported'); //
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 );
});
});
}除了iOS上的Safari支持服务人员和web推送API之外,所有主流浏览器。虽然Safari在Mac OS上支持推送通知,但目前(2019年),iOS上没有浏览器支持推送通知
桌面上的Safari也支持使用专有技术APNs(而非推送API标准)的网站通知。iphone上的chrome怎么样?我甚至无法在那里注册服务人员,我在那里收到“服务人员不受支持”警报