Cordova Background Fetch ajax request not completing and another not starting

Viewed 69

Im currently using Cordova Background Fetch to retrieve any notices for the user and launching the notifications locally. But while the app plugin is launched the ajax request remains pending and the second one doesnt launch, I assume its because of the 30sec timeout.

Can I get any assistance on the code below or any other alternatives that can achieve that goal.

Background Fetch Plugin https://www.npmjs.com/package/cordova-plugin-background-fetch

The code

async function fetchBackgroundNotices(){

var BackgroundFetch = window.BackgroundFetch;

// Your BackgroundFetch event handler.
var onEvent = async function(taskId) {
    
    console.log('[BackgroundFetch] event received', taskId);
    // Required: Signal completion of your task to native code
    // If you fail to do this, the OS can terminate your app
    // or assign battery-blame for consuming too much background-time
    
    var userdata = getUserData(); 
    if(userdata !== null){
        
        //set notice count badge
        $.ajax({
            url: "https://platform.followups.co.ke/api/business/notices/getcount/" + userdata.agenciesid,
            tryCount : 0,
            retryLimit : 3,
            dataType: 'json',
            error: function (){

                this.tryCount++;
                if (this.tryCount <= this.retryLimit) {

                    //try again
                    $.ajax(this).done(embedNoticeCountBadge);
                    return;
                }   

                return;
            },
            method: "GET"
        }).done(embedNoticeCountBadge);
        
        //get the notice messages
        $.ajax({
            url: "https://platform.followups.co.ke/api/business/notices/getunread/" + userdata.agenciesid,
            tryCount : 0,
            retryLimit : 3,
            dataType: 'json',
            data: {
                fetch: BackgroundFetch,
                taskid: taskId
            },
            error: function (){

                this.tryCount++;
                if (this.tryCount <= this.retryLimit) {

                    //try again
                    $.ajax(this).done(function(response){
                        pushNoticeMessages(response);
                        
                        //run the finish background task
                        this.data.fetch.finish(this.data.taskid);
                    });
                    return;
                }   

                return;
            },
            method: "GET"
        }).done(function(response){
            pushNoticeMessages(response);
            
            //run the finish background task
            this.data.fetch.finish(this.data.taskid);
        });
    }
};

// Timeout callback is executed when your Task has exceeded its allowed running-time.
// You must stop what you're doing immediately BackgroundFetch.finish(taskId)
var onTimeout = async function(taskId) {
    
    console.log('[BackgroundFetch] TIMEOUT: ', taskId);
    BackgroundFetch.finish(taskId);
};

var status = await BackgroundFetch.configure({minimumFetchInterval: 15}, onEvent, onTimeout);
console.log('[BackgroundFetch] configure status: ', status);

}

This is how its shown on the console enter image description here

0 Answers
Related