网络推送库无法在Google Chrome中创建网络推送通知,错误类型为“(ArrayBuffer或ArrayBufferView)”

时间:2019-01-10 12:33:02

标签: java google-chrome mozilla web-push

我想从Java库(称为Java的Web-push库)发送推送通知。它是firefox中的工作文件,但是当涉及到Chrome浏览器时,它无法通过以下方式订阅推送管理器。

无法在“ PushManager”上执行“订阅”:提供的值不是“(ArrayBuffer或ArrayBufferView)”类型

这是项目的代码和我的文件夹结构。

在我尝试为谷歌浏览器使用相同的代码之前,它无法正确执行。那个时候它不会授权Mozilla Firefox,那是我发现的服务器端错误。

async function subscribe() {
const response =  await fetch('./VapidPublicKey');
const vapidPublicKey =  await response.text();
console.log(vapidPublicKey);
    const convertedVapidKey = urlB64ToUint8Array(vapidPublicKey);
    var browserOptions={};
    if(typeof InstallTrigger !== 'undefined'){
       browserOptions={userVisibleOnly: true};
    }else{
          browserOptions={
               userVisibleOnly: true,
               applicationServerKey: convertedVapidKey
           };   
    }
       navigator.serviceWorker.ready.then(function 
    (serviceWorkerRegistration)
       {
       serviceWorkerRegistration.pushManager.
        subscribe(browserOptions).then(function 
        (subscription) {
              return sendSubscriptionToServer(subscription);
             })
             .catch(function (e) {
                if (Notification.permission === 'denied') {
                   console.warn('Permission for Notifications was denied');
                    } else {
                  alert('Unable to subscribe to push.'+ e);
                 }
            });
        });
        }
        </code>
       </pre>'
    ##And below is code for conversion from base64url to uint8Array ##'
    <pre><code>
      async function urlB64ToUint8Array(base64String){
        const padding = '='.repeat((4 - base64String.length % 4) % 4);
      const base64 = (base64String + padding)
      .replace(/\-/g, '+')
     .replace(/_/g, '/');

      const rawData = window.atob(base64);
      const outputArray = new Uint8Array(rawData.length);
      for (let i = 0; i < rawData.length; ++i) {
         outputArray[i] = rawData.charCodeAt(i);
       }
      console.log("output array is : "+outputArray);
         return outputArray;
        }
     </code>
     </pre>

Folder structure


RootFolder
       |-WebCOntent
            |-index.html
            |-main.js
            |-sw.js
            |-manifest.json

The exception I am getting

TypeError: Failed to execute 'subscribe' on 'PushManager': The provided value is not of type '(ArrayBuffer or ArrayBufferView)'

I have Resolved above type error below code worked for me

const urlB64ToUint8Array = (base64String)=>{ const padding = "=".repeat((4 - base64String.length % 4) % 4); const base64 = (base64String + padding).replace(/\-/g, "+").replace(/_/g, "/"); const rawData = window.atob(base64); const outputArray = new Uint8Array(rawData.length); for (let i = 0; i < rawData.length; ++i) { outputArray[i] = rawData.charCodeAt(i); } return outputArray; }

0 个答案:

没有答案