javascript后台同步android电源按钮 [英] javascript background sync android power button

查看:53
本文介绍了javascript后台同步android电源按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 android chrome 中不会触发 service worker 同步我错过了什么???同样的事情在这里:JavaScript 后台同步如果页面在离线时加载/刷新,则停止工作

service worker sync will not fire on case in android chrome WHAT IM MISSING ??? Same thing in here: JavaScript Background Sync Stops Working if Page is Loaded/Refreshed while offline

  1. 拔下 USB 电缆进行开发(用于 chrome 开发工具)
  2. 按下安卓主页按钮
  3. 按下电源按钮进入睡眠状态(黑屏)
  4. 然后我等待 5 分钟(至少)
  5. 然后我按下电源按钮唤醒
  6. 然后我将 myapp 推送到唤醒应用程序(应用程序未关闭但设置为睡眠)
  7. 然后我插入 USB 电缆进行调试
  8. 然后我单击某个按钮以启动同步,然后我看到 ---

在控制台注册"中但同步"事件未触发如果我再次单击电源按钮(黑屏)然后我再次单击电源按钮 - 然后它会醒来或者我关闭wifi然后打开wifi - 然后它会醒来

in console "REGISTER" but "SYNC" event not fired if i click power button again (black screen) and then i click power button again - then it will wake up or i turn off wifi and then turn on wifi - then it will wake up

然后在控制台注册"中看到和同步"

AND then is see in console "REGISTER" and "SYNC"

如果我等待 30 分钟,那么注册的同步将被删除

IF i wate 30 minutes then registered sync is deleted

客户端JS

var id = 'background-sync-' + (new Date()).getTime() + $.uniqid(6);    
let postData = {
                        'type': 'background-sync',
                        'uuid': id,
                    };
                    registration.active.postMessage(postData);

服务人员方面

self.addEventListener('message', function (event) {

    //console.log('message=' + event.data.type);
    if (event.data.type === 'background-sync') {

        registerSync(event.data.uuid, event.data);
    }
});

self.addEventListener('sync', function (event) {

    console.log('SYNC');
    //event.waitUntil(syncIt(event.tag));
});


function registerSync(uuid, data) {

    if (data != undefined && data != null) {

        let id = data.id;

        //append data to storage
        syncStore[id] = data;
        //this is important
        syncStore[id]['sending'] = false;
    }

    console.log('REGISTER');
    //console.log(data);
    //console.log(self.registration.sync.getTags());

    self.registration.sync.register(uuid)
        .then(function () {
        })
        .catch(function (err) {
            return err;
        });
}

推荐答案

没有解决方案.甚至后台定期同步也不起作用.所以我已经解决了 - 如果可能,发送常规帖子并使用 background.sync 作为离线数据同步.问题是如果我点击电源按钮一次然后同步请求设置为等待如果第二次,则同步请求将发送到服务器.

There no solution. Even background-periodic-sync does not work. So i have made work around - send regular post if possible and use background.sync as offline data sync. Problem is if i click power button once then sync request set to wait AND if second time then sync request will be sent to server.

解决方案代码:

if( this.online ){

    let self = this;
    $.post(self.requestUrl, self.params, function (returnData) {

        if( callback )
        callback(returnData);
    }).fail(function () {

        window.backgroundSync(self.requestUrl, self.params, callback, self.storeData);
    });
}
else {

    window.backgroundSync(this.requestUrl, this.params, callback, this.storeData);
}

这篇关于javascript后台同步android电源按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆