gpt4 book ai didi

javascript - PushManager 不请求移动设备上的许可

转载 作者:行者123 更新时间:2023-12-02 22:42:23 24 4
gpt4 key购买 nike

我正在尝试向我的网站实现推送通知,我设法在桌面上做到这一点,但是在移动设备上不会提示请求用户许可。

我尝试过在 iPhone 上使用 safari、iPhone 上的 Chrome(甚至无法在那里注册 Service Worker)、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 标准)的网站通知。

关于javascript - PushManager 不请求移动设备上的许可,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58540418/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com