Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-23972] iOS: BgService more than 10 minutes and show blue bar issue for Location.

GitHub Issuen/a
TypeBug
PriorityMedium
StatusClosed
ResolutionInvalid
Resolution Date2016-10-02T13:05:36.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsiOS
LabelsallowsBackgroundLocationUpdates, backgroundservice, ios, ipass1, location
ReporterMotiur Rahman
AssigneeHans Knöchel
Created2016-10-01T20:28:59.000+0000
Updated2018-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

Comments

  1. Hans Knöchel 2016-10-01

    *app.js*:
       var win = Ti.UI.createWindow({
           backgroundColor: '#fff'
       });
       
       // Create a Button.
       var bgService = Ti.UI.createButton({
           title: 'StartService'
       });
       
       // The event-listener needs to be outside the background-service
       Ti.Geolocation.addEventListener('location', function(e) {
           if (e.error) {
               alert('Error: ' + e.error);
           } else {
               Ti.API.info("value=" + JSON.stringify(e.coords));
           }
       });
       
       function bgServiceStart() {
           Ti.Geolocation.allowsBackgroundLocationUpdates = true;
           Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_BEST;
           Ti.Geolocation.preferredProvider = Ti.Geolocation.PROVIDER_GPS;
       
           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() {
           // If you havr the AUTHORIZATION_ALWAYS permissions, the blue bar will not be shown
           if (!Ti.Geolocation.hasLocationPermissions(Ti.Geolocation.AUTHORIZATION_WHEN_IN_USE)) {
               Ti.Geolocation.requestLocationPermissions(Ti.Geolocation.AUTHORIZATION_WHEN_IN_USE, function(e) {
                   if (!e.success) {
                       Ti.API.error("Location permissions not granted: " + e.error);
                       return;
                   }
       
                   bgServiceStart();
               });
           } else {
               bgServiceStart();
           }
       });
       
       win.add(bgService);
       win.open();
       
    *bgservice.js*:
       Ti.API.info('bg-service1: service page');
       
       function getCoords() {
           Ti.Geolocation.getCurrentPosition(function(e) {
               Ti.API.warn(e);
           });
       }
       
       setInterval(function() {
           getCoords();
       }, 10000);
       
    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 the AUTHORIZATION_WHEN_IN_USE permission and the NSLocationWhenInUseUsageDescription 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.
  2. Hans Knöchel 2016-10-02

    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.
  3. Rainer Schleevoigt 2016-11-05

    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 and NSLocationWhenInUseUsageDescription Don you see way?
  4. Rainer Schleevoigt 2016-11-06

    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?
  5. Eric Merriman 2018-08-06

    Closing as invalid. If incorrect, please reopen.

JSON Source