【问题标题】:pushManager not asking for permisson on mobile devicespushManager 未在移动设备上请求许可
【发布时间】: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


    【解决方案1】:

    目前(2019 年)iOS 上没有浏览器支持推送通知。

    桌面版 Safari 还支持使用称为 APNs 的专有技术(而不是 Push API 标准)向网站发送通知。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • iphone 上的 chrome 怎么样?我什至不能在那里注册服务人员,我在那里收到“不支持服务人员”的警报
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-03
      • 2013-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多