Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Background task even when the app is closed

zakzakzakzak INMember ✭✭

Hi All,

i am building an Xamarin Forms App which retrieves location data from the device when its running as background service and when its connected to the server it would send the gps data to the server. The functionality of the app must not stop when the app is closed. Can you guys guide me through the process, or provide me some reference or sample code to do this. I am really struggling to make the app run in the background when its closed.

Regards,
Zak

Tagged:

Posts

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @zakzak
    You want your code to run... When your code isn't running. Is that what I'm reading?
    Not going to happen. Certainly not in iOS, that's for sure.
    Under UWP and Droid you can make services and/or listeners. But if your app is closed, its closed: Your code isn't running.

    Background operations are a complex topic that have different rules on different platforms. You probably should do the Xamarin University classes: iOS210 and AND210 - Those are the intro to background tasks. Hopefully you took advantage of the free 60 days of XamU for downloading Visual Studio 2017 during the first week.

    On a personal note: This sounds a lot like spyware

  • voidstreamvoidstream FRMember ✭✭✭
    edited March 2017

    @ClintStLaurent
    I'm agree, but i understand what he want to do. GPS Tracking its really interesting for technician who need to go on more sites (for example). I think you should never stop the app? Or an app can start another app (gps tracker only?)?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @voidstream
    Oh I get that. There are lots of valid reasons for GPS tracking... From vehicle/fleet management... to turn-by-turn directions... Bike/hike/trail tracking... All valid.

    But when someone says "I want to track the user even when my app isn't running" - then it looks like spying. Any of the valid reasons for tracking the user also involve the app running and the user aware of it. As you alluded to: Why not just keep the app running if you're not doing something nefarious?

  • zakzakzakzak INMember ✭✭
    edited March 2017

    Hi all,

    Thanks for your quick turnaround on this... Have found one solution implemented here, but it's not in Xamarin.

    http://mobileoop.com/getting-location-updates-for-ios-7-and-8-when-the-app-is-killedterminatedsuspended

    Can you see if i can achieve this in Xamarin? Also the main reason why we don't want to show the app or even run in the background is the user closes them all the time, no matter how hard we ask them not to do they close them.

    More over it is a app only for employees on company phone....

  • zakzakzakzak INMember ✭✭

    Anyone please reply on this :(

  • zakzakzakzak INMember ✭✭

    Thanks Vinayak for the sample application, would go through and let you know.

  • TarunOhriTarunOhri USMember ✭✭

    Hi did anyone get the solution to it ? The Xamarin link given above stops the location updates as soon as the app terminates. Also, i have got another link which works good when you switch to home screen or press home button twice or switch to another app. But as soon as you cancel/close the app the location service stops running.
    https://developer.xamarin.com/guides/ios/application_fundamentals/backgrounding/ios_backgrounding_walkthroughs/location_walkthrough/
    Can anyone provide solution to it ? I want constants updates of a user irrespective if the app is open or close.

  • DaveUDaveU GBMember ✭✭

    Hi. I'm also looking to run a service which updates my app's data and puts it in local storage every x minutes, even if the app has been killed (i.e. force removed by the user from the list of backgrounded apps) or if the app has not been started. Is this simply not possible? It seems I could use silent notifications for this from Firebase etc but I'd rather not go down that route.

  • Hi,
    You could try using the AlarmManager from Android. (i cannot post link yet, but just google it )
    Let it start a background service that initiates gps tracking.

  • Chasy_AChasy_A Member ✭✭
    edited March 2018

    Hi,
    I tried everything(!!!) and still no success, I need the same exact thing as @zakzak
    Please, did any of you manage to implement such thing?

  • gioxgiox Member ✭✭
    edited March 2018

    I need it too!! ;) and exactly for the same reason, it's an application for company internal use, to be installed on smartphones to monitor the fleet and help workers to reach nearest clients

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I think a lot of you are confusing "Background task" with "service".

    A background task requires your app to be running. Period. That's how your code is running in the background.

    If you want something running all the time even when a given app isn't running, then you're talking about a service that launches when the device starts up. But understand: You still have your code running. Its just in a startup service that has no UI.

    Maybe that clarification will help y'all do some better research now that you understand you're looking for service not background task.

    @giox said:
    I need it too!! ;) and exactly for the same reason, it's an application for company internal use, to be installed on smartphones to monitor the fleet and help workers to reach nearest clients

    Then why does it need to be in the background and hidden? Why isn't this fleet app always running? Its a company app on a company device. Use something like Samsung Knox or some other MDM to properly setup the device so your app is the only thing running - IE: Make it the launcher and don't allow anything else to be launched. Then your tablet basically runs like an embedded device.

    If you're creating an enterprise-grade app then it should be treated with enterprise-grade solutions like property MDM.

  • gioxgiox Member ✭✭

    @ClintStLaurent Definitely more clear! Many thanks! Now I need to study how to create services.

    Why isn't this fleet app always running?
    Actually, it's more complicated:
    They have several applications on the smartphone and depending on the work they are performing they start one or another.
    And then don't forget that someone will try everything to shut down the app. I'll check Samsung Knox if it can help me on that.
    And then also they can use also outside their working hrs, therefore I have to shut down the GPS tracker to start again the morning after.
    Many thanks again

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @giox said:
    They have several applications on the smartphone and depending on the work they are performing they start one or another.

    But they should all be part of the company product line. The company 'main' app gets set as the launcher for the device. When the user presses the home button they will always be taken to the main company app.
    If the main company app is the device launcher, then they can launch those other company apps from the main app. Thus they can only have access to the approved company apps.

    This means your app is always running. If you want a background task in that such as GPS tracking, its not a problem.

    And then also they can use also outside their working hrs,

    Eh... Bad company policy in my opinion but nothing you can do to change the minds of the bosses.

    therefore I have to shut down the GPS tracker to start again the morning after.

    Not in my opinion. If it is a company-owned device you track it. Period. If the employee doesn't show up for work you can get your hardware back. If they don't want the company to know they went to a strip-club while off-duty then don't take the company-owned hardware. Employees need to understand its not their device.

  • gioxgiox Member ✭✭

    But they should all be part of the company product line. The company 'main' app gets set as the launcher for the device. When the user presses the home button they will always be taken to the main company app.
    If the main company app is the device launcher, then they can launch those other company apps from the main app. Thus they can only have access to the approved company apps.

    This means your app is always running.

    Pretty interesting this solution! I don't know it was possible without rooting the device. Sorry but I need to learn a little bit more about mobile programming... :/

    therefore I have to shut down the GPS tracker to start again the morning after.

    Not in my opinion. If it is a company-owned device you track it. Period. If the employee doesn't show up for work you can get your hardware back. If they don't want the company to know they went to a strip-club while off-duty then don't take the company-owned hardware. Employees need to understand its not their device.

    Here in Italy, there is a very very very strong protection about the privacy and employees rights. Just think that for tracking the GPS during working hours through the company devices you must provide a well valid and proven necessity! I know it's crazy!! :D

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @giox said:
    Pretty interesting this solution! I don't know it was possible without rooting the device.

    Yep. Root access is not required to set your app as a launcher.

    https://stackoverflow.com/questions/4841686/how-to-make-a-launcher

  • zakzakzakzak INMember ✭✭

    @ClintStLaurent said:

    @giox said:
    Pretty interesting this solution! I don't know it was possible without rooting the device.

    Yep. Root access is not required to set your app as a launcher.

    https://stackoverflow.com/questions/4841686/how-to-make-a-launcher

    Thank you all for your valuable time on replying to my post, Anything on the iOS level we can run service without main app running?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    No. iOS is far more strict. I strongly suggest the Xamarin University courses on backgrounding. It sounds like it is a major requirement for your app and doing it through "guess and by-golly" isn't a strategy.

  • arturo1212arturo1212 Member ✭✭
    edited July 2018

    I have been working on a similar project and i managed to send location data to a server even when the user closes the app using Services. I sucessfully implemented it on Android, but i can't test it on iOS.
    I mainly followed arteksoftware tutorial for backgrounding, but found a lot of usefull info in other posts. (Sorry, i'm a newbie here and can't post links)
    For your problem with privacy protection, you could check if your phone current time is in the worktime range before sending any location data to the server (that's what i do), you could even check against an online time API if you want to guarantee that the user is not changing the phone's time. Another option could be handle this on the server side.

    PD: (Android) If you don´t want to use firebase or similar for keeping your service alive you can usea trick with local notifications. This has some disadvantages but "I't works". I'm working on a tutorial but, for now, I can share code segments if u need.

  • gayugokhalegayugokhale INMember ✭✭

    @ClintStLaurent Suppose I am creating multiplayer game. when I go to the background in the middle of the game, what iOS actually do is, queue up the actions that other players have already played. and show it one by one when I reopen the app. but I get not breakpoint hit or written in console, when the actions are getting queued(In the background). Then where these actions are stored ? How can I access it ? I have 90% code ready working in both Android and iOS, except this issue in iOS. Please help.

    Thanks.

Sign In or Register to comment.