[IONIC 5] Angular http request even when app is closed

Hello there,

I have studied about work manager in android and how does it works, I am having problem for running a javascript function inside Cordova java file. How can we put angular http function inside the dowork function of android work manager, I want to run angular http request in background when app is completely closed. Does anyone know how can this be done?

If u refer to service workers, then I don;t think you can put angular code in a service worker. It must be plain vanilla JS. So you may want to be looking for plain vanilla JS code examples that do the trick. And obviously the service worker is capable of doing http.

But maybe I am wrong and is angular/pwa more versatile

Thank you for this.

I have tried plain javascript alert function inside Cordova Plugin java file, the app did not show the alert. I don’t know why but the same javascript alert function worked when I tried it in simple app created from android studio. Any idea why javascript is not working inside Cordova java files?

Hi

in Java files u need to use Java, not javascript. So a JS alert will definitely not work in a java cordova source file.

I am not a java expert, so if you want the cordova plugin in java to do UI stuff, u need to figure out how to do that in java.

And in parallel figure out why from an architecture perspective at all you want the plugin to do UI, because ideally you put that in the TS code. Remember, the plugin can communicate with the TS code (once transpiled into JS)…

Yes, that is correct.

I don’t want to perform UI functions from plugin either. I want to communicate to Typescript function from cordova plugin. Is there any example on how we can call function declared in typescript from Cordova plugin?

Almost every cordova plugin has this. So it is just a matter of deep diving in the java code to look for the bridge to javascript (named cordova) as well as the JS/TS definition in the @ionic/native wrapper.

If that is too cryptic for you, that you should consider not going this route, as a big chunk of discovery on your own is part of the journey.

Example: https://github.com/apache/cordova-plugin-camera

Function execute is where the magic starts

I do not completely got you but I will look more into the ionic native wrapper.

What I actually meant, I want to call a function declared in ionic app Service TS file (utilityService.ts) from the Cordova Plugin.

In the Cordova Plugin I have created a service in Java which runs in interval in android. I want to add the function declared in (utilityService.ts) files into the Java service function.

Look at the geolocation plugin which has a function to update the web app on location via callvack/observable

That should you how to bridge from a change in the plugin to the web apo

Yes I have looked into that, It provides the geo location in response and location permission info.

It did not help as I don’t want result of my Java work manager service, I want to hook my javascript function into the java service function so that whenever the java service runs when app is closed it will execute my javascript function too.

I want to make an http request even when my ionic app is closed. How can we do that? I have tried background mode and other background plugins but all of them works to keep app function running when app is in background not when ionic app is closed. I have looked into the official documentation of Android for this and I found out about Android work manager which keeps running even when app is closed. I created Cordova Plugin with the work manager but I am not able execute javascript function inside this work manager. Any idea how can we achieve this?

I don’t want to derail the discussion, but I see a ton of topics with this same overall goal, and my first instinct is to always think like a user.

Mobile OSes exist in sort of a hybrid land between embedded and desktop systems, and while technology like Ionic allows developers to really embrace the notion that one can deploy virtually the same codebase to mobile and desktop, we have to also keep in mind that we need to be good citizens of the mobile environment.

Which includes the fact that battery life is precious, and asleep means asleep. Having applications be constantly waking the device in spite of user power management preferences is IMHO a very consequential rudeness. So unless we are developing an app that will only be deployed in environments where power management is categorically a non-issue, I think it is incumbent on us as developers for moral reasons not to even attempt technological circumvention of task scheduling.

If you can use push notifications to achieve your overall goal, I think that would be a substantially better route to take.

1 Like

If the app is closed the I dont think the webview exists anymore so all web assets are disposed? So calling would not work anyway?

Either way, not my cup of tea and to the the previous point: a truckload of (recent) posts re background processes and the likes of it exist in this forum

To me it seems something a web app does not deliver fully like a full native

Thank you for the in detail explanation. I agree to all of it, still if you know about android work manager which allows to perform tasks like image upload can still execute when app is closed. Take a look at the documentation of android work manager. I am confused if it is possible in android will it come to ionic too or can be done in ionic too?