How to run javascript code when the app is on the background with webview_flutter?

28 views Asked by At

I have a very important problem.I know that there are many posts about it, but I haven't found any solution.

I am coding an app that use the plugin webview_flutter to display a webpage with a list and a button to refresh the list of items.The goal of my app is to automatize the searching of items. Here are the steps of my research: -get the items list -click on the refresh button -get the new items list -compare the old one and the new one to know if there is a new item on the list -If there is a new item, stop the searching process and send a message to the javascript channel -if not, re click on the refresh button

I have a javascript code that works well when the app is at the foreground. but when the app goes to the background, there is a problem.The android app works well, but the iOS version background searching works only for 20 secondes.

Here is the code that is about background research:

    // Function to handle the app lifecycle state. Triggers when the app switches foreground or background.  
    
    @Override  
    void didChangeAppLifecycleState(AppLifecycleState state) {  
    super.didChangeAppLifecycleState(state);  
    context.read\<AppProvider\>().setForeground(state == AppLifecycleState.resumed);  
    var researchState = context.read\<AppProvider\>().research;  
    if (state == AppLifecycleState.resumed && researchState) {  
    webController.runJavaScript("isBackground = false");  
    }  
    if (state == AppLifecycleState.paused && researchState) {  
    webController.runJavaScript("isBackground = true");  
    backgroundResearch();  
    }  
    }

    // Function to run the background research (startResearch() from Provider does not work in     the background).  
    Future\<void\> backgroundResearch() async {  
    var stopOnNewLoad = context.read\<AppProvider\>().stopOnNewLoad;  
    var stopOnPayoutChange = context.read\<AppProvider\>().stopOnPayoutChange;  
    var payoutThreshold = context.read\<AppProvider\>().payoutThreshold;  
    var operationOnLoad = context.read\<AppProvider\>().operationOnFirstLoad;  
    while (context.read\<AppProvider\>().research && !context.read\<AppProvider\>().isForeground) {  
    await Future.delayed(Duration(milliseconds: 3000 + (Random().nextInt(50))), () {  
    webController.runJavaScript("""searchForLoad($stopOnNewLoad, $stopOnPayoutChange,         $payoutThreshold, "$operationOnLoad")""");  
    });  
    }  
    }


here is my javascript code :

    searchForLoad = async (stopOnNewLoad, stopOnPayoutChange, payoutThreshold, OperationOnLoad) =\> {
    console.log("searchForLoad");
    await waitForPageLoad();
    let difference = \[\];
    let newList = \[\];
    if (isBackground) {
    newList = await getAllLoad();
    difference = FindDifference(
    oldList,
    newList,
    stopOnNewLoad,
    stopOnPayoutChange,
    payoutThreshold
    );
    if (
    difference.length \> 0 &&
    difference\[0\].childDivId !== undefined &&
    difference\[0\] !== "error" &&
    research
    ) {
    research = false;
    let childDivId = difference\[0\].childDivId;
    actionAfterLoadFound(
    childDivId,
    difference\[0\].payoutDifference ?? 0,
    OperationOnLoad
    );
    window.app_channel.postMessage(
    typeOfResult +
    "#" +
    difference\[0\].title +
    "#" +
    difference\[0\].totalPayout +
    "#" +
    difference\[0\].payoutDifference ?? 0
    );
    } else if (research) {
    clickOnRefresh();
    }
    }
    JSON.stringify(difference);
    };

The problem happens on TestFlight (iOS test) and doesn't on debug running. The searching loop has a delay of 1ms to 5sec so I cannot use Background Fetch or WorkManager

I tried to verify if the problem is the webview, or the app. It tried to verify how much time the background process works. I conclude that the app works for 20 secondes after the beginning of the background start.

0

There are 0 answers