服务工作者在Chrome Android上意外注销-服务工作者在Chrome Android上不是永久的

时间:2019-03-08 08:18:41

标签: android google-chrome push-notification firebase-cloud-messaging service-worker

在nodejs上运行服务器,因为我遵循Google教程(并使用' node-gcm '包)跳转到有效负载Firebase通知,所以一切正常。我的Android手机(Android 8.0.0上的Galaxy A3 2017)上的注册正在运行,我确实收到通知,但它不是永久的。 几天后,我的机器不再注册。它在台式机chrome上效果很好,因此我不会怀疑服务器端。

下面是客户端上的代码

var reg;
navigator.serviceWorker.register('sw.js').then(function() {
        return navigator.serviceWorker.ready;
        }).then(function(serviceWorkerRegistration) {
        reg = serviceWorkerRegistration;
        console.log('Service Worker is ready :^)', reg);
        checkSubscription();
                    }).catch(function(error) {
                    console.log('Service Worker Error :^(', error);
            });
        };
function urlBase64ToUint8Array(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 (var i = 0; i < rawData.length; ++i) {
    outputArray[i] = rawData.charCodeAt(i);
  }
  return outputArray;
}

 const vapidPublicKey = '<Your Public Key from generateVAPIDKeys()>';
const convertedVapidKey = urlBase64ToUint8Array(vapidPublicKey);


function subscribe() {
    if (reg){
        reg.pushManager.getSubscription().then(function(pushSubscription) {
            if (pushSubscription==null){
                reg.pushManager.subscribe({
                    userVisibleOnly: true,
                    applicationServerKey: convertedVapidKey
                    }).then(function(pushSubscription) {
                    var sub = pushSubscription;
                    console.log('Subscribed!', sub);
                    socket.emit("notification subscription payload",sub);
                    isSubscribed = true;
                }).catch(function(error){
                    alexUI.view.log.addLog({severity:"ERROR"},clientSocketID,"Erreur de souscription");
                    alexUI.view.radioButton($("#subscription"),"Off");
                });
            }else{
                var sub = pushSubscription;
                // socket.emit("notification subscription",sub.endpoint);
                socket.emit("notification subscription payload",sub);
                isSubscribed = true;
            };
        }).catch(function(e){
            // to remove message later Alex
            alexUI.view.log.addLog({severity:"ERROR",message:e},clientSocketID,"Notifications indisponibles");
            alexUI.view.radioButton($("#subscription"),{set:"Off"});
        });
    }else{
        alexUI.view.log.addLog({severity:"ERROR"},clientSocketID,"Navigateur incompatible");
        alexUI.view.radioButton($("#subscription"),"Off");
    };
};

1 个答案:

答案 0 :(得分:0)

清洁手机存储器后,问题消失了。服务人员无法在手机内存已满的情况下正确加载。