Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
944 views
in Technique[技术] by (71.8m points)

service worker - javascript background sync android power button

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. unplug usb cable for development (for chrome dev tool)
  2. push android home button
  3. push power button for sleep (black screen)
  4. then i wait 5 minutes (at least)
  5. then i push power button for awake
  6. then i push myapp to wake app (app is not closed but set to sleep)
  7. then i blug in usb cable for debugging
  8. then i click some button to fire sync after that i see ---

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"

IF i wate 30 minutes then registered sync is deleted

CLIENT SIDE JS

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

SERVICE WORKER SIDE

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;
        });
}
question from:https://stackoverflow.com/questions/65942049/javascript-background-sync-android-power-button

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

solution code:

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...