[TIMOB-23972] iOS: BgService more than 10 minutes and show blue bar issue for Location.
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | Medium |
Status | Closed |
Resolution | Invalid |
Resolution Date | 2016-10-02T13:05:36.000+0000 |
Affected Version/s | n/a |
Fix Version/s | n/a |
Components | iOS |
Labels | allowsBackgroundLocationUpdates, backgroundservice, ios, ipass1, location |
Reporter | Motiur Rahman |
Assignee | Hans Knöchel |
Created | 2016-10-01T20:28:59.000+0000 |
Updated | 2018-08-06T17:37:01.000+0000 |
Description
Steps to Reproduce.
1. Create a classic project.
2. paste the following code to the corresponding file.
var win = Ti.UI.createWindow({
title : 'Background Services Example',
backgroundColor : '#4186cd',
modal : true
});
// Create a Button.
var bgService = Ti.UI.createButton({
title : 'StartService',
height : Ti.UI.SIZE,
width : Ti.UI.SIZE,
top : 20,
});
function bgServiceStart() {
Ti.API.info('Registering background services');
var service = Ti.App.iOS.registerBackgroundService({
url : 'bgservice.js'
});
Ti.API.info('*** Press home button to pause application ***');
}
// Listen for click events.
bgService.addEventListener('click', function() {
bgServiceStart();
alert('click bg service');
});
// Add to the parent view.
win.add(bgService);
win.open();
Ti.API.info('bg-service1: service page');
if (Ti.Platform.name == "iPhone OS" && parseInt(Ti.Platform.version.split(".")[0]) >= 8) {
Ti.App.iOS.registerUserNotificationSettings({
types : [Ti.App.iOS.USER_NOTIFICATION_TYPE_ALERT, Ti.App.iOS.USER_NOTIFICATION_TYPE_SOUND, Ti.App.iOS.USER_NOTIFICATION_TYPE_BADGE]
});
}
function pushData(latLong) {
var curNotif = Ti.App.iOS.scheduleLocalNotification({
alertBody : latLong,
date : new Date(new Date().getTime() + 1000)
});
}
function getCoords() {
if (Ti.Geolocation.locationServicesEnabled) {
Ti.Geolocation.allowsBackgroundLocationUpdates = true;
Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_BEST;
Ti.Geolocation.preferredProvider = Ti.Geolocation.PROVIDER_GPS;
Ti.Geolocation.addEventListener('location', function(e) {
if (e.error) {
alert('Error: ' + e.error);
} else {
Ti.API.info("value=" + e.coords);
pushData(e.coords.latitude + '\n' + e.coords.longitude);
}
});
}
}
setInterval(function() {
getCoords();
}, 10000);
<key>UIBackgroundModes</key>
<array>
<string>location</string>
</array>
<key>NSLocationAlwaysUsageDescription</key>
<string>Specify the reason for accessing the user's location information.
This appears in the alert dialog when asking the user for permission to
access their location.</string>
<key>UISupportedInterfaceOrientations~iphone</key>
3. Run that code to the device
4. It does not work more that 10 minutes even below 10 minutes and don't show blue bar.
5. It should work more that 10 minutes and show blue bar as well since I have added UIBackgroundModes key
- http://docs.appcelerator.com/platform/latest/#!/guide/tiapp.xml_and_timodule.xml_Reference-section-29004921_tiapp.xmlandtimodule.xmlReference-UIBackgroundModes
*app.js*:
*bgservice.js*:
If you have the
AUTHORIZATION_ALWAYS
permissions, the bar will not be shown (since the user already agreed to always check the location). So you need theAUTHORIZATION_WHEN_IN_USE
permission and theNSLocationWhenInUseUsageDescription
key only. After that, the bar will be shown when you put your app in the background. *EDIT*: And the app just ran 15+ minutes, so the first problem also looks invalid after the made changes. So to note: The (proper) background-service itself it used to stay active > 10 minutes and the permissions are used in conjunction with the background-modes to show the "blue bar" Please give it a try. Resolving for now.In addition, we may place this code (with some more refactoring and documentation) in the [Background Services Guide|docs.appcelerator.com/platform/latest/#!/guide/iOS_Background_Services] and link that to the
allowsBackgroundLocationUpdates
docs.I used your proposed code and the service will killed after 3 minutes. I start from app.js with
require("bgservicestarter")();
content of bgservicestarter: https://gist.github.com/AppWerft/a31fdf98132077c37ae33809938c917f and here the service: https://gist.github.com/AppWerft/c3dd3b7b2d00da2bd91e034156dcb8d1 In tiapp.xml is the UIBackgroundModes key andNSLocationWhenInUseUsageDescription
Don you see way?Hi Hans, I think we need generally AUTHORIZATION_ALWAYS permission? The other is only for foreground - or? And I think Ti.Geolocation.allowsBackgroundLocationUpdates = true; Ti.Geolocation.preferredProvider = Ti.Geolocation.PROVIDER_NETWORK; Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_THREE_KILOMETERS; Ti.Geolocation.trackSignificantLocationChange = true; is better then Ti.Geolocation.allowsBackgroundLocationUpdates = true; Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_BEST; Ti.Geolocation.preferredProvider = Ti.Geolocation.PROVIDER_GPS; But I'm not sure. In my app it doesn't work in both cases. The app will kill after ca. 200 sec. Any ideas why?
Closing as invalid. If incorrect, please reopen.