I have used the system notification plugin from the github(https://github.com/saileshmittal/phonegap-system-notification-plugin) for android phonegap.I have used this code in my index.html
My code is:
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
var not_title = 'Message';
var not_text = 'Zou dit werken?';
var not_tText = 'Message';
navigator.systemNotification.onBackground();
navigator.systemNotification.onForeground();
navigator.systemNotification.createStatusBarNotification(not_title, not_text, not_tText);
}
I got the notification icon in both foreground and background.But is it possible to run the whole application to run in the background when the button is clicked in the application and call my wcf services continuously.Also i need to get the alert even when running in the background.how to do that?
navigator.systemNotification.onBackground():this line run the application in background or not other wise it only for showing the notification after closing the application.
please guide me,thanks in advance.
I made an Android application with PhoneGap and when I press the home button, my application stays in background (with my last test it stays more than 1 hour). But when I launch other App the Android OS kills my application.
To enable this behaviour you need to add the android permission:
<uses-permission android:name="android.permission.WAKE_LOCK" /> to your manifest
and you also need to add:
<preference name="exit-on-suspend" value="false" /> to your config.xml in folder res/xml
I hope this post can you help you :)
According to the phonegap documentation you cannot run the apps in the background, the js cannot continue to run. The reason notifications are able to work is because they are using the native system to send the notifications.
In order to make processes run in the background you will need to create a plugin that uses the native os.
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 have a very simple Phonegap application that loads an external website:
<script type="text/javascript" src="cordova.js"></script>
<script>
function onDeviceReady() {
if (navigator.connection.type == Connection.NONE) {
navigator.notification.alert('An internet connection is required to continue');
} else {
window.location = "http://example.com";
}
}
document.addEventListener("deviceready", onDeviceReady, false);
</script>
When I run it it all works properly and loads the external site. If I switch to a different app and then click the icon to run it (in Android) it switches back to the already running application with no issues.
The problem started occurring when I switched from my testing environment to my production one and switched it to go to an https version. Now, when I click on the icon it restarts the application instead of simply switching back to the application already running.
Is there any way to control whether clicking on the icon reloads the application or simply switches back to the already running application?
EDIT:
Okay, I thought the only difference was changing the "http" to "https", but apparently I also did an upgrade of "phonegap". I tried switching it back to "http" and it's still doing the same wrong thing. Is there any way to control this? I'm currently running 6.5.2 and I think the proper functionality was with 6.5.0 .
EDIT 2:
Alright... It seems that it's somewhat random. I have it running the latest and using "https" and it sometimes reloads and sometimes doesn't.
Mobile operating system OS's each have different ways of managing memory in apps. The general consensus is that the app should always "save early/often" in case it's killed by the OS to free up memory, etc.
In this case, there may be a way though. Try out the Android Launch Mode preference:
<preference name="AndroidLaunchMode" value="singleInstance" />
I've never used this, so perhaps try out each mode too.
I am writing a simple Phonegap application for Android. This program will send notification to notification bar and make the phone vibrate periodically.
I use navigator.notification.vibrate(time_period) to achieve the target. According to this article, both beep and vibration are not supported by android emulator. Hence, I was expecting that there could be entry indicating failure of it in the Catlog, but there is no such entry. The question is how to make sure that a vibration event has happened or failed (without deploying to a device).
AppHarbor looks like one of the ways to debug Phonegap application remotely. I wonder if there is other local ways to test Phonegap application as an HTML5 website in a Chrome browser (navigator.notification call is a standard call)? If yes, then it is probably possible to somehow parse the browser's console automatically to find out if the vibration event has happened.
Can you hide the vibrate() call behind an abstraction which you can replace depending on which platform you are using?
For example
var vibrateFunc = function(time_period) {
if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
console.log('vibrating for ' + time_period)
} else {
navigator.notification.vibrate(time_period)
}
}
and then have your app code call vibrateFunc() whenever it wants to vibrate.