iOS: Start uploading user location after request from server - ios

I need to achieve this: I have an app, which wants to keep track of location of family members. Since I don't want to keep the GPS running all the time, I was thinking I could just send a request from server, when some family member wants to know my location.
But I ran into some problems:
I can't use PUSH Notifications, because those need to be confirmed (tapped on) and only after that the app knows something happened. (This would not be great, since kids probably wouldn't tap the push notifications)
The other option would be to keep the app running in the background, checking server for any news and if it found some request on server only then start the GPS tracking and upload coordinates to server. This sounds a bit better, but also battery draining.
Did anyone already try this? Is there any better way to this problem?
Thanks for any reactions! :)

Yes, I think the only option for you left is run Location Services in background.
For this in Plist set for Required background modes to App registers for location updates. This will enable you to send location updates as you want.
But note that this will only work when application is running or in background, and it will not run when you application is closed. If user closes your application, then you can fire push notification if you done get any updates from device.
Also, note that you need to mention in description of your application, that Application uses location services which will drain your battery, otherwise your application will not be approved.
Hope this info helps you..

We had similar problem in our application. We followed the approach that keep listening to push notification port, whenever we receive any notification, then check if it's for our application and then react accordingly.
We can have listening to port on long time to save battery draining.
Sorry cannot share any of the code with you, but I hope this might be some help.

Related

What is my best bet on periodically running a task to update Live Activity when my app is in the background?

I have an idea for an app that utilises Live Activities, inspired by the Flightly app (it starts a Live Activity for a plane ride): Basically I wanna use open data from an HTTP endpoint that is reachable via the onboard WiFi on a bus. E.g. you board the bus, connect to the onboard WiFi, open the app, it fetches details about the stops and then you start a Live Activity guiding you to a selected stop. So far so good. But sometimes the bus faces delays, etc. In that case, I need to update the Live Activity. That is easy when my app is running. It is not so easy, when my app is not running (or running in the background).
What is my best approach to regularly fetching data from the endpoint and updating my Live Activity? The problem is, that the endpoint is only reachable on the buses WiFi. So it looks like I cannot use APNS to push updates to the Live Activity (since my service running outside the bus cannot access the endpoint that provides the details and thus cannot push any meaningful updates). Background Tasks (e.g. background app refresh) run infrequently.
What options are left? I thought about receiving location updates in the background and acting on them to update my app state (and possible the Live Activity, if needed), although I haven't investigated that path in detail, yet.
Has anybody faced a similar issue and found a solution or can provide some guidance on how to approach this problem?
I think the easiest option is core location. So keep your application running by monitoring the position and then you can update the Live Activity by fetching the information from your access point. It should be fine for app review since you could use the the location to show a position on a map and only while the person is on the bus.
If your application isn't running you have to send the ActivityAttributes via APNS. Since the data cannot be accessed from a remote server that would communicate with APNS there is no direct way to use ActivityKit push notifications. An indirect approach (which I would not recommend) would be to send silent push notifications triggers every 5min or so and fetch the newest information when the application is woken up. But this won't work when the app is force-quit and using silent pushes to trigger polling will give you a bad score by the system and eventually the frequency will be throttled from APNS.

Continue running code in background for Sending Notification to a Single User

I need to notify user when a certain condition is met but that condition might or might not meet in months. I need to run condition check in background (even when app terminates) so that I could detect condition is met (even if it happens after 3 months). If condition is met then I need to send notification only to a single user (not all users).
I am not sure what is the best way of doing it. Local Notification or Push Notification? I can not schedule a local notification in advance because I don’t know when it will meet the condition. I think Push notification is not needed because I am sending only one notification to only one user in several days/months.
I also need to constantly run this check in background. I know it can be run infinitely in background by using location update (significant monitoring or background navigation). But my condition checking code doesn’t actually require location updates. As far as I understand, Apple allows only location update or network related task in background (for few minutes). That's why I am thinking of putting my code in background location update code.
In my opinion , I should use local notification and put my code in background location update code so that I could check constantly if certain condition is met. This background check will schedule local notification and show it to the user who is using that device. I don’t think it is possible to schedule a push notification by code running in background.
In summary, I have following 3 questions
Should I put my code in background location update? If not, what is the right approach of running my code infinitely in background without location update?
Will Apple reject application if I put my code in background location update while location update code doesn’t actually send/receive current location?
What kind of notification should I use in this kind of situation where I send notification to a single user after months? Would you use Local Notification? or Push Notification.
Please reply. Any help is truly appreciated.
Thanks in advance.
This is a bit of a tricky problem unfortunately.
Apple won't allow you to create long-running operations in the background. As you mentioned, you can add code in the background with location updates or audio, but neither of those are very good solutions since (1) Apple will likely reject you from the app store and (2) the user will probably close the app if they notice a big locations banner at the top of their phone (or a weird background audio signal), and closing the app will kill your operation. For any truly long-running tasks it's probably best to set up a server and run a cron job or equivalent long-running operation. It's the only way you can know that the task will continue running.
Sorry, I was just answering one at a time, but as I mentioned above, yes, they'll probably reject your code for that. As a rule of thumb: if you think the Apple-created code you're using wasn't meant for what you're using it for, Apple will probably reject your app.
It sounds like you should use a remote notification. If you do use a remote notification, you'll probably want some sort of backend anyway. You could use a local notification to notify a user after a few months, but you would need to know the exact time to send it up front, which it doesn't sound like you would.
In summary: Try to build a simple BE. Maybe use Firebase Functions or something, and also build an APNS system to send pushes. In the app, tell the server to start processing the information and send the push back when it's done. The situation sounds like a perfect use case for server-side logic.
Hope that helps!

IOS/SWIFT use gps ,internet and calculation on the background

As a new iOS developer I am need to ask if it possible to run a background service on the phone when the application close and in this service to use user location, send the location to server and the return data compare to another data (for some calculation) and also send to the user Local Push Notification and all of this from the background ?
It's definitely possible to do all those things. Here's some info to start you off with location:
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html
and notifications:
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/WhatAreRemoteNotif.html
Both documents discuss how these things work in general, but also talk briefly about how they work in the background. Hopefully this gives you a jumping off point to find what you need.

iOS: Notify a WebService of location change (using Significant-Change Location Service)

I'm developing an app that allows me to track the device's location, the idea is that the app notifies every few hours our servers of the current location so I can send relevant push notifications to the user.
This should work even if the app is not running (the app should be awakened by the OS).
I was reading the Location Awareness Programming Guide (http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html) and I think the Significant-Change Location Service would help me accomplish this, however the guide mentions this:
it should do minimal work and avoid any tasks (such as querying the
network) that might prevent it from returning before the allocated
time expires.
If this is the case, I don't think I will be able to notify our servers of the location change.
I'm assuming this is possible because there are apps like "Find My Phone" that have to get the updated location of the devices every once in a while.
If this is not the right approach I would appreciate any pointers to info on what should I do to get this working.
Thanks

Running IOS App with Screen Locked

I am working on a sports app that uses GPS and OpenEars text-to-speech. The app delivers speed and other GPS info to the user audibly (via earphones) so the user (skier, cyclist, etc) can get the GPS info without looking at the screen. In order to save battery life, I would like the app to run in the background with the screen off. Is this possible with IOS? I tried setting "Required backgrund modes" for both GPS and audio, but my app quits working when the screen is off.
thanks..
One one to get around it is to have a silent audio file playing, when there is no audio, it might get your app rejected in the app store though... here is a question that talks about the silent audio file playing to keep in the background... It might get through app store approval in your situation since it is an app that relays on audio as one of its main functionalities..
You can subscribe to GPS info change notifications and your app will run when something needs to be updated. Check this.
Please note this sentence:
"If you leave this service running and your application is subsequently suspended or terminated, the service automatically wakes up your application when new location data arrives. At wake-up time, your application is put into the background and given a small amount of time to process the location data. Because your application is in the background, it should do minimal work and avoid any tasks (such as querying the network) that might prevent it from returning before the allocated time expires. If it does not, your application may be terminated."
There's no way to do it as service except some special cases provided by ios. But you can simulate it. This post answer how to:
https://stackoverflow.com/a/19121250/2835520
I hope it's gonna help!

Resources