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.
Related
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'm deploying my Meteor (1.2.0.2) app to an Android device.
I installed the plugin by
meteor add cordova:cordova-plugin-geolocation#1.0.0
Added Cordova plugin cordova-plugin-geolocation#1.0.0.
I used the plugin by:
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
navigator.geolocation.getCurrentPosition(function(p){ // onSuccess
console.log(p);
}, function(e){
console.log(e);
});
}
On desktop works perfectly (print p as Geolocation object) but on mobile it just ignores the callbacks (both success and errors).
I tested it in Chrome DevTools (chrome://inspect) by setting breakpoints. It steps until .getCurrentPosition call but then (either step over or step in) just skips both callbacks (and If I set a breakpoint within them, they are just not hit).
I even tried
navigator.geolocation.getCurrentPosition(function(p){ console.log(p);
both on Desktop console (works)
undefined
Geolocation { ... }
and mobile (does not work)
undefined
// nothing else is printed
Maybe has it something to do with app's permissions?
EDIT: I verified AndroidManifest.xml and the permissions are actually set by the plugin:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
#dragonmnl,
GPS is unreliable.
You did not talk about timeout, I must assume you have NOT set the timeout long enough. Try setting it to about 20 seconds -- as a minimum.
If your readings are poor, or you are timing out
go outside
make sure you have a clear view of all horizons (nothing blocking the view)
walk in a big circle, about 15ft in diameter
In about 30-45 seconds you should have a reading.
This will make sense when you read the link I gave you.
Jesse
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
document.addEventListener("backbutton", backButtonEvent, false);
}
function backButtonEvent() {
var currentUrl=window.location.hash;
if(currentUrl=='#/homePage' || currentUrl=='#/'){
navigator.app.exitApp();
}
else{
history.go(-1);
navigator.app.backhistory();
}
}
Iam using ionic framework and phone gap.. here when i click device backbutton from home page its just minimizing that app not exiting. How can i exit my app??
In case you mean by minimizing that app that the application goes to the background (is not the active application but it is still in the list of running applications). In case that is what you mean, please notice that this is the normal behavior in Android and many Mobile OS.
Terminating (exiting if you prefer) the application is not under the control of the application developer. It is completely managed by the OS (Android in this case). So, what you have is the normal behavior for apps under Mobile OS.
Instead of terminating the application as you would do under old OS (Linux, Windows and alike), you have to manage the lifecycle of the application: pause, resume, ...
Install plugin https://github.com/ZhichengChen/cordova-plugin-android-home.
Implement 2 handlers for plugins.
Use the bellow code to exit the application.
exitApp:function() {
app.exitApp();
}
i'm trying to get the geolocation on Android Browser but nothing happens. I'm using a Samsung Galaxy S3 but i'm not sure about the version of my browser. Android version is 4.1.2
Here is my Code:
if (navigator.geolocation) {
var timeoutVal = 10 * 1000 * 1000;
navigator.geolocation.getCurrentPosition(
displayPosition,
displayError,
{ enableHighAccuracy: true, timeout: timeoutVal, maximumAge: 0 }
);
}
this is a code i copied and pasted from this site
it gives me the "navigator.geolocation"
but when it comes to "getCurrentPosition" my code stops working. Mobile Chrome works fine but this is not. I shared my position but still nothing happens. Any help will be appriciated.
Thanks.
Thanks everyone i found the solution,
i was getting the geolocation after some javascript operations. I tried to get the geolocation before document is ready. And it worked.
I know this is a bit old but it keeps coming up on searches so I thought I would add a tip that helped me.
Because I want to get the location right as the page loads I found that I needed to introduce a very short delay after the page loads. When I had no delay, I would get no error but also I would not activate the location protocols on the phone. This half second delay solved the issue. You can play with the delay and see if it solves your issues.
setTimeout(function() {getAutoLocation(true)},500);
I get the location in my "getAutoLocation(true)" function. This setTimeout only exists to introduce the delay.
seems like PhoneGap has a problem with geolocation
I have the same issue
I'm using S3 with Android 4.1.2, phonegap geolocation feature doesn't work
In order to get the geolocation without errors, you have to make that code block work before using the values provided by the geolocation because operations are carried out asynchronously, in this question i found the solution by loading my geolocation script before other .js files. This solved my problem and another trick for this issue is, geolocation works more stable when you give "always" permission for browser to read your location. After loading for the first time, you never encounter geolocation errors.
I found that some Android phones (old and new) don't run properly the function
getCurrentPosition, maybe trying to save some battery.
I played with the function watchPosition and then the high accuracy GPS kicked in.
Read this to know how to use the parameters properly:
http://dev.w3.org/geo/api/spec-source.html#watch-position
In my case, this worked:
{
maximumAge: 0, timeout: 2000, enableHighAccuracy: true
}
Hope this helps someone.
Did you give Internet permission in manifest?
<manifest xlmns:android...>
...
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>