I am developing an mobile application (platform: Android, iOS) with Cordova.
My application needs ping an URL to fetch data every hours. I want my application still ping the URL when it's closed.
I searched in google and I get some of this plugins:
https://github.com/katzer/cordova-plugin-local-notifications
https://github.com/katzer/cordova-plugin-background-mode
I need a plugin like the second one but also work when the application is closed like scheduled notification like the first one.
Is there any plugin like this for cordova? Or it's impossible to do background task like this with cordova.
Thank you
I had some what the same issue, i needed to pick lat,lng every few minutes and calcuate the distance, but background plugin alone couldnt solve it, as it stops working when the phone goes to sleep .. so I had to make sure, the phone doesn't go to sleep ..
So i used power management plugin together with Background mode plugin.. and it works well..
Background mode Plugin:
https://github.com/katzer/cordova-plugin-background-mode
Power Management Plugin
https://github.com/boltex/cordova-plugin-powermanagement
if( ionic.Platform.isAndroid() ){
cordova.plugins.backgroundMode.enable();
window.powerManagement.dim(function() {
console.log('Wakelock acquired');
}, function() {
console.log('Failed to acquire wakelock');
});
window.powerManagement.setReleaseOnPause(false, function() {
console.log('setReleaseOnPause successfully');
}, function() {
console.log('Failed to set');
});
}
Related
I am developing an app which uses an embedded system connected to through Bluetooth to an embedded device and I want the app to run a function when the device sends a signal.
The issue is that I cannot keep the app open all the time and thus need the app to keep running in the background and keep listening for the signal send by the embedded device so that it can run the function I want.
I have seen the following questions :
How to run flutter app in background continually?
How can I make my flutter app run in background without stopping?
How to create a service in Flutter to make an app to run always in background?
and many more and so far nothing seems to work.
I have written this code for now :
Future<void> _run_app_in_background() async {
final config = FlutterBackgroundAndroidConfig(
notificationTitle: 'MEDICA',
notificationText:
'MEDICA is running in the background',
notificationIcon: AndroidResource(name: 'background_icon'),
notificationImportance: AndroidNotificationImportance.Default,
);
var hasPermissions = await FlutterBackground.hasPermissions;
hasPermissions = await FlutterBackground.initialize(androidConfig: config);
final backgroundExecution =
await FlutterBackground.enableBackgroundExecution();
}
but it seems to be doing nothing and the same with other packages.
So how can I make my app run in background to keep listening for the Bluetooth data sent by device.
Edit:I want this to run on both Android and iOS and if possible maybe someone can suggest me another framework apart from Flutter (in comments maybe) and I can search the internet for the same.
#HrishabhNayal Hello bro Try this way!
https://medium.com/dlt-labs-publication/flutter-run-code-in-the-background-461b4d6c635b
I need some assistance to make my App working in background.
I created my first App with Cordova 10, jquerymobile and it works fine excepted in backgound.
The idea is to installed the following plugin cordova-plugin-background-mode, but I get some difficulties to understand how.
I installed the plugin and then I added the following
document.addEventListener('deviceready', onDeviceReady, false);
function onDeviceReady() {
document.getElementById('deviceready').classList.add('deviseIsReady');
cordova.plugins.backgroundMode.enable();
if(Pages.checkConnection() == true)
{
Maps.load();
setInterval(Maps.load, 300000);
}
}
In that way, the mobile is ready, the background mode is actived. I upload my App to my Android but my App stop working.
I wonder, if this is would be a solution
$(window).load(function(){
document.addEventListener("offline", Pages.offLine, false);
document.getElementById("refresh").addEventListener("click", Maps.refresh);
document.getElementById("ffield").addEventListener("change", Maps.field);
document.getElementById("fstations").addEventListener("change", Charts.changeStation);
document.getElementById("threshold").addEventListener("change", Pref.threshold);
document.getElementById("ffieldpref").addEventListener("change", Pref.field);
cordova.plugins.backgroundMode.on('EVENT', backgroundEvent);
});
function backgroundEvent(){
if(cordova.plugins.backgroundMode.isActive()){
cordova.plugins.backgroundMode.moveToBackground();
}
else
{
cordova.plugins.backgroundMode.moveToForeground();
}
}
But I do not know when, it will be actived and if it will work with IOS devise.
Some of you have an experience with Cordova and how to setup the background mode with jQuery Mobile? Any examples?
Many thank for your help and suggestion.
Cheers
You cannot use katzer's cordova-plugin-background-mode with Cordova 10. Works fine on 9.0, but that's as high as it goes. If your Builder program allows you a choice of Cordova versions, pick 9. I have been unable to find an adequate substitute for the plugin that works in 10.
Also in Android, check your config.xml or AndroidManifest.xml and make sure there is FOREGROUND_SERVICE permission. This is absolutely required for background operation.
I’m using local notifications native plugin on my ionic 4
project (latest version), but when I click on notification and my app is closed the click event is not triggered. It works when app is in background or foreground in ios device.
I use local notifications inside a provider and my on click code is inside its constructor but when app is closed it's not working. I’ve tried to write code inside platform ready in app/app.component.ts but this approach does not work. This is my code:
this.platform.ready().then((readySource) => {
this.localNotifications.on('click').subscribe(noti)=> {
alert("ok");
});
});
I have the same issue. I have tried to call it inside platform ready in home.ts constructor. But still no success. I have IOS 12 in my iPad device. This works perfectly when Ionic app is running in background.
But it does not work when App is killed and not opened.
Is it something related to event life-cycle?
https://blog.ionicframework.com/navigating-lifecycle-events/
I am developing an ionic app. I am wondering if there is any event handler for this condition when the app is being stop/close in follow ways:
1) Settings -> Application -> Application Name -> Force stop
2) Swipe to close app.
I would expect something like this. But the best i could found is to cater for app exiting via $ionicPlatform.registerBackButtonAction which does not include the above two scenario.
Also, I am not sure whether this can be done in native platform as well. Please advise on this.
angular.module('app').run(function($ionicPlatform) {
$ionicPlatform.onAppStopOrForceClose(function() {
// Any function here
});
});
ionic.Platform.exitApp(); // stops the app
window.close();
see: http://ionicframework.com/docs/api/utility/ionic.Platform/
if you try in ios: you can't exit an IOS App, Apple do not allow apps to exit programmatically
I've developed a socket app (realtime) for Android. Everything was working fine until the Android 6 update with brings the "doze" mode. Is there a plugin that prevent from dozing? This renders my app useless since when the app goes in doze mode, the app cannot use the network connection anymore. I'm running a background mode plugin but this isn't enough, doze takes over.
Thank you.
edit: following Emanuel's comments, I have found this post about it but no valid answer.
How do I add my app to the whitelist so it doesn't stop by "doze" ? I cannot find any info anywhere... except this doc, but doesn't say how to add to the whitelist. Since my app doesn't rely on GCM, I should be good, if only I can find how to add my app!
use this cordova plugin to White listing an Android application programmatically from battery optimize settings
To install
cordova plugin add https://github.com/thomas550i/cordova-plugin-doze-Optimize
Javascript code of usage
cordova.plugins.DozeOptimize.IsIgnoringBatteryOptimizations(function (responce){
console.log("IsIgnoringBatteryOptimizations: "+responce);
if(responce=="false")
{
cordova.plugins.DozeOptimize.RequestOptimizations(function (responce){
console.log(responce);
}, function (error){
console.error("BatteryOptimizations Request Error"+error);
});
}
else
{
console.log("Application already Ignoring Battery Optimizations");
}
}, function (error){
console.error("IsIgnoringBatteryOptimizations Error"+error);
});
There is no plugin that prevent from dozing
But Users can manually configure the whitelist in Settings > Battery > Battery Optimization. Alternatively, the system provides ways for apps to ask users to whitelist them.
An app can fire the ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS intent to take the user directly to the Battery Optimization, where they can add the app.
check this : https://developer.android.com/training/monitoring-device-state/doze-standby.html