Uploaded image for project: 'Titanium SDK/CLI'
  1. Titanium SDK/CLI
  2. TIMOB-14658

iOS7: Background Service no longer functions

    Details

    • Type: Bug
    • Status: Closed
    • Priority: High
    • Resolution: Cannot Reproduce
    • Affects Version/s: Release 3.1.1
    • Component/s: iOS
    • Labels:
    • Environment:

      iPhone, Titanium 3.1.1 GA

      Description

      Since iOS 7 beta 3, using a background service with a setTimeout poll time, no longer runs after 1 iteration, after that it just seems to stop.

      Global that initiates the service

      var service;
       
      if (Titanium.Platform.name == 'iPhone OS'){
      	// Globe event handelers
      	Ti.App.addEventListener('pause', function(e){ 
      		if (Ti.App.Properties.getBool('track')) {
      			service = Ti.App.iOS.registerBackgroundService({url:'/service/bg.js'});	
      		}
      	});
      			
      	// Stop background service on launch.
      	Ti.App.addEventListener('resume', function(e){
      		
      		if (service != null) {
      			service.stop();
      			service.unregister();
      		};
      	});
      };
      

      bg.js

      // Background service script
      // NOTE: Runs in background when enabled by user to track device and report back to ETMS system.
      // This is for use by company security departments to ensure safety of user.
       
       
      	if (!Ti.Geolocation.locationServicesEnabled) {
      		Ti.App.iOS.scheduleLocalNotification({
      		    alertBody: 'Location services are not enabled.',
      		    date:new Date(new Date().getTime() + 1000) // 1 second after unregister
      		}); 
      	}
      		
      	//var tracking = require('business/tracking');
      	
      	var lastTrackDate = 0;
      	var doTrack = false;
      	
      	function track() {	
      			trackGPS('track');
      			lastTrackDate = new Date().getTime();
      			setTimeout(track, Ti.App.Properties.getInt('trackingpolltime'));
      	};
      	track();
       
      	var nowTime;
      	var stayaliveInerval = 311000;
      	var nextstayalivepoll;
      	function stayalive() {
      		nowTime = new Date().getTime();
      		if (lastTrackDate + stayaliveInerval <= nowTime) {	
      			trackGPS('update');
      		}
      		nextstayalivepoll = (nowTime - lastTrackDate) > stayaliveInerval ? stayaliveInerval : stayaliveInerval - (nowTime - lastTrackDate);
      		Ti.API.info(new Date() + ' - GPS Stay Alive:' + nextstayalivepoll);
      		setTimeout(stayalive, nextstayalivepoll);
      	}
      	stayalive();
      	
      	
      	// Event listener for GPS
      	function trackGPS(trackType)
      	{
      		if (trackType == 'track') {
      			doTrack = true;
      		}
      		else {
      			doTrack = false;
      		};
      		
      		Ti.Geolocation.addEventListener('location', getPosition);		
      	};
      	
      	
      	function getPosition(e) {
      	    if(e.error){
      	    	Ti.API.info(new Date() + ' - GPS error');
      	    }
      	    else {
      	    	Ti.Geolocation.purpose = "Determine device location on users request.";
      	    	Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_HIGH;
      		    Ti.Geolocation.distanceFilter = 0;
      		  
      		    Ti.Geolocation.getCurrentPosition(function(e){
      		        if(e.error){	
      		        Ti.API.info(new Date() + ' - GPS error');
      		        }
      		        else {
      			        //Get the GPS goods
      			        var longitude = e.coords.longitude;
      			        var latitude = e.coords.latitude;
      			        var altitude = e.coords.altitude;
      			        var accuracy = e.coords.accuracy;
      			        var timestamp = e.coords.timestamp;
      			        
      			        if (accuracy < 50) {
      			        	Ti.API.info(new Date() + ' - GPS accuracy good,  background event listener:' + e.coords.accuracy);
      						
      					if (doTrack == true) {
      						Ti.API.info(new Date() + ' - Contacting web service');
      						SendTrackingInfo(e.coords.latitude, e.coords.longitude);
      					}; 
      					
      					Titanium.Geolocation.removeEventListener('location', getPosition);	
      			        };
      		        };
      		    });
      	    }; 
      	};
      	
      	var client = null;
      	
      	function SendTrackingInfo(latitude, longitude) {
      	
      	 	client = Ti.Network.createHTTPClient({
      		    onload : function(e) {Ti.API.info(new Date() + ' - Background web service conected');},
      	   		onerror : function(e) {Ti.API.info(new Date() + ' - Background web service NOT conected');},
      	   	 	timeout : 28000
      		});
      		
      		// Prepare and send the connection.
      		client.open("GET", 
      			//'http://192.168.0.23/tracking/TrackingService/Track?deviceid=' +
      		//	Ti.App.Properties.getString('deviceid') +
      		//	'&longitude='+longitude+'&latitude='+latitude+'&clientid=' +
      		//	Ti.App.Properties.getString('clientid') +
      		//	'&trackingtypeid=1');
      		
      	//	client.send();
       
      	alert('Service Called');
       
      	};
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                ingo Ingo Muschenetz
                Reporter:
                anvil_martin Martin Williamson
              • Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Backbone Issue Sync

                  • Titanium SDK/CLI <> Titanium Mobile
                    Synced with:
                    TIMOB-17072
                    Sync status:
                    ERROR
                    Last received:
                    Last sent:

                    Git Source Code