Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 三星互联网推送通知服务人员注册的持续时间_Javascript_Firebase_Push Notification_Service Worker - Fatal编程技术网

Javascript 三星互联网推送通知服务人员注册的持续时间

Javascript 三星互联网推送通知服务人员注册的持续时间,javascript,firebase,push-notification,service-worker,Javascript,Firebase,Push Notification,Service Worker,我正在尝试构建一个web应用程序,它将向订阅它的用户发送推送通知(在三星互联网上测试)。但是,我面临一个问题,几个小时后,手机停止接收推送通知,我需要重新打开web应用程序并重新订阅以恢复接收推送通知。以下是我的服务人员及其注册的代码: function subscribeUser() { const applicationServerKey = urlB64ToUint8Array(applicationServerPublicKey); swRegistration.pushMana

我正在尝试构建一个web应用程序,它将向订阅它的用户发送推送通知(在三星互联网上测试)。但是,我面临一个问题,几个小时后,手机停止接收推送通知,我需要重新打开web应用程序并重新订阅以恢复接收推送通知。以下是我的服务人员及其注册的代码:

function subscribeUser() {
  const applicationServerKey = urlB64ToUint8Array(applicationServerPublicKey);
  swRegistration.pushManager.subscribe({
    userVisibleOnly: true,
  })
  .then(function(subscription) {
    console.log('User is subscribed.');

    updateSubscriptionOnServer(subscription);

    isSubscribed = true;

    updateBtn();
  })
  .catch(function(err) {
    console.log('Failed to subscribe the user: ', err);
    updateBtn();
  });
}
服务人员:

var windowActive = true;
var numMessages = 0;
var sAdder;


self.addEventListener('push', function(event) {
  console.log('[Service Worker] Push Received.');
  // console.log(`[Service Worker] Push had this data: "${event.data.text()}"`);

  console.log(windowActive);

  numMessages++;

  if(numMessages == 1) {
    sAdder = "";
  } else {
    sAdder = "s";
  }

  var title = 'Convers8te';
  var options = {
    body: numMessages + ' message' + sAdder + ' received!',
    icon: '/images/logo/8-icon.png',
    badge: '/images/badge.png',
    tag: 'c8t',
  };

  if(windowActive == false) {
    event.waitUntil(self.registration.showNotification(title, options));
  }

});

self.addEventListener('notificationclick', function(event) {
  console.log('[Service Worker] Notification click Received.');

  event.notification.close();
  numMessages = 0;

  event.waitUntil(
    clients.openWindow('')
  );
});

self.addEventListener('message', function (evt) {
  windowActive = evt.data['windowActive'];
  console.log('postMessage received', evt.data);
});
注册:

function subscribeUser() {
  const applicationServerKey = urlB64ToUint8Array(applicationServerPublicKey);
  swRegistration.pushManager.subscribe({
    userVisibleOnly: true,
  })
  .then(function(subscription) {
    console.log('User is subscribed.');

    updateSubscriptionOnServer(subscription);

    isSubscribed = true;

    updateBtn();
  })
  .catch(function(err) {
    console.log('Failed to subscribe the user: ', err);
    updateBtn();
  });
}
服务器上的更新订阅然后将推送令牌发送到服务器,以便通过Firebase服务使用

我尝试了一些方法,比如延长会话持续时间,并在其他几个设备上进行测试,但它似乎在几个小时后停止接收推送通知

另外,一个附带的问题是,即使三星Internet explorer应用程序关闭,推送通知是否也可以工作?目前,它们只在选项卡关闭时对我有效,但在整个应用程序关闭时不起作用

感谢您抽出时间阅读并提供帮助