pushManager 不在移动设备上请求权限

pushManager not asking for permisson on mobile devices

我正在尝试向我的网站实施推送通知,我设法在桌面上做到了,但是 在移动设备上没有询问用户权限的提示。

我试过在 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 );

  });

});

}

除 iOS 上的 Safari 外,所有领先的浏览器都支持服务工作者和网络推送 API。虽然 Safari 确实支持 Mac OS

上的推送通知

目前(2019 年)iOS 上的浏览​​器均不支持推送通知。

桌面上的 Safari 还支持使用称为 APNs 的专有技术而非 Push API 标准的网站通知。