【发布时间】:2019-10-24 11:30:44
【问题描述】:
我正在尝试向我的网站实施推送通知,我设法在桌面上做到了,但是 没有提示要求用户在移动设备上获得许可。
我试过在 iphone 上使用 safari,在 iphone 上使用 chrome(甚至不能在那里注册服务人员),在 iphone 上使用 dadduckgo 浏览器,但没有在桌面上正常工作的弹出窗口。
我的代码:
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 );
});
});
}
【问题讨论】:
标签: javascript mobile push-notification notifications web-push