简体   繁体   English

成功注册后,期间后台同步不会触发

[英]Period background sync does't fire after successfully registered

I'm trying to make periodic bg sync where service worker updates badge.我正在尝试在服务工作者更新徽章的地方进行定期 bg 同步。 When I run my page and test it via Chrome DevTools, Service worker process the request.当我运行我的页面并通过 Chrome DevTools 对其进行测试时,Service worker 会处理请求。 But when the page is closed, it doesnt't do anything.但是当页面关闭时,它什么也做不了。 Same on mobile phone.手机上也一样。

On my page (this part is working and output in console is periodic update set ):在我的页面上(这部分正在运行,控制台中的 output 是periodic update set ):

navigator.permissions.query({name:'periodic-background-sync'}).then(function(result) {
  if (result.state === 'granted') {
    console.log('periodic background granted');
    navigator.serviceWorker.ready.then(function(registration){       
      if ('periodicSync' in registration) {
        try {
          registration.periodicSync.register('update-badge', {
            minInterval:  60 * 60 * 1000,
          }).then(function(ret){
            console.log('periodic update set');
          });
        } catch (error) {
          console.log('Periodic background sync cannot be used.');
        }
      }
    });
  }
});

Service worker:服务员:

async function updateBadge() {
  const unreadCount = 5; //fixed value for testing 
  navigator.setAppBadge(unreadCount).catch((error) => {
      console.log('Error setting badge.');
  });
}

self.addEventListener('periodicsync', (event) => {
  if (event.tag === 'update-badge') {
    event.waitUntil(updateBadge());
  }
});

So when I manually fire background sync from DevTools, badge is set, but not automatically in the background as I thought it will work.因此,当我从 DevTools 手动触发后台同步时,徽章已设置,但不会自动在后台运行,因为我认为它会起作用。

I can't find anything wrong with the code.我找不到代码有什么问题。 I've seen variants where you request periodic-background-sync after navigator.serviceWorker.ready but I think that both work (especially since you can trigger the registered event manually).我已经看到您在 navigator.serviceWorker.ready 之后请求 periodic-background-sync 的变体,但我认为两者都有效(特别是因为您可以手动触发已注册的事件)。

I think the problem is that not all conditions for periodic background sync to fire are true.我认为问题在于并非所有周期性背景同步触发的条件都是真实的。 These are the conditions from the initial implementation in Chrome (2019):这些是 Chrome(2019)初始实施的条件:

  • Chrome version 80 or later Chrome 版本 80 或更高版本
  • The PWA needs to be installed需要安装PWA
  • The website needs to have an engagement score (can be viewed here chrome://site-engagement/ ).该网站需要有一个参与度分数(可以在这里查看chrome://site-engagement/ )。 Websites with 0 engagement is deleted from the list and won't trigger the periodicsync event, even if it is installed.参与度为 0 的网站将从列表中删除,即使已安装,也不会触发 periodicsync 事件。 Once a user hasn't interacted with a website for 2 hours, its engagement levels begin to decay.一旦用户 2 小时未与网站互动,其参与度就会开始下降。 With minimal engagement, a periodicsync event will be triggered at an interval of 36h.在最小参与度的情况下,periodicsync 事件将以 36 小时的间隔触发。 source 来源

In your case I think your code is correct and working but you haven't interacted enough with you app continuously over 36h so the engagement is purged and periodicsync fire timer cancelled (if you have installed your PWA).在你的情况下,我认为你的代码是正确的并且可以工作,但是你没有在 36 小时内与你的应用程序进行足够的交互,所以参与被清除并且 periodicsync 触发计时器被取消(如果你已经安装了你的 PWA)。

For the record, here is a complete working demo (event registration) (and sw code ).作为记录,这里有一个完整的工作演示(事件注册) (和sw 代码)。

The specific interval at which the periodicsync event is fired varies;触发periodicsync事件的具体时间间隔各不相同; what I've seen on, eg, installed Android web apps is that you'll get periodicsync around once a day.我所看到的,例如,安装了 Android web 应用程序是您每天大约会获得一次periodicsync同步。 It may be different on desktop platforms (and will only happen if your browser is actually running at the time).在桌面平台上可能会有所不同(只有当您的浏览器当时实际运行时才会发生)。

Have you waited a full day or so after installing your PWA, and see if it's fired?您是否在安装 PWA 后等待了一整天左右,然后查看它是否已被解雇?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM