How to get background notifications and data sync

I have some requirements and now I want to check if these requirements are possible and how they can be achieved. It is about notifications and works in the background:

  • The app should show notifications if new data is available and the app is in the background (if the app is not running, there should be no notifications)
  • Also, the application must sync the data in order for the data to be updated
  • The user should be able to select the interval when synchronization should be performed (5 seconds, 30 seconds, ...)
  • The app must work on iOS 6 or higher.

Notifications:
The problem with local notifications is that you can only set seconds, minutes, hours ... as an interval. Unable to set custom spacing. But this type would only be relevant if I have a background task looking for updates and if an update is available it immediately displays a local notification.

Remote notifications on the other hand need an additional server for notifications. Also port 5223 should be open to receive notifications (but most of the time the cellular network is used, and here the port should be open). How many remote notifications can be sent and presented to the user? For local notifications, this is 64.

Run in the background:
Available only in iOS 7 Background Fetch

and Remote Notficiations

. The problem with Background Fetch

is that only small data needs to be transferred, and the OS determines when the application receives a time slot (a proprietary algorithm that learns from the user). Sources report max. runtime from 30 seconds, but I have not found an official statement from Apple.

Remote Notifications

also seems to need a provider. But here the user will receive a notification and the app can download / sync new data in the background (again 30 seconds maximum).

How about iOS 6?

Another option would be to run the app in the background / have a background thread that checks for updates / sync on a specific interblock and creates a local notification. But how long does the app run in the background? I read about 5 seconds to keep it short.

And one more is to use push notifications, and if the user launches the app, it will complete the sync process.

beginBackgroundTaskWithExpirationHandler

doesn't seem to fit my case.

How do I get notifications while running in the background along with syncing?

+3


source to share


1 answer


As you probably know, background work on iOS is pretty limited. iOS will prevent your app from running in the background indefinitely. If you do beginBackgroundTaskWithExpirationHandler

, your app will get some background execution (10 minutes on iOS 6, 3 minutes on iOS 7), but you can't "refresh" this time while the app is still in the background. So your second option ("let the app run in the background / have a background thread that checks for updates / syncs at a specific interval") is impractical unless you want to abuse other background modes (which AppStore most likely won't succeed review process).

Since iOS 7, it has become possible to send "silent" push notifications (ie, those that have a content-available

set), for example. notifications that will be delivered directly to the app (instead of showing an alert and only launching the app when the user confirms it). This makes a significant difference to the background and is actually quite handy. Whenever your app receives such a push notification, it can check for new data, sync data, etc. (You get about 30 seconds of execution time). Apple says it suppresses "silent" push notifications, but there isn't much detail on this. In my practice, sending them every 15 minutes seems to work.

You could argue that 30 seconds is a way to sync data up a bit. It's true. To fix this problem, iOS 7 has loaded downloads and downloads that you can initiate with NSURLSession

, configured with [NSURLSessionConfiguration backgroundSessionConfiguration:]

. The download / upload will be handled by the system, and once it completes, your application will be woken up for processing.



As far as I know, this is the best approach you can use to sync data from background apps regularly. However, this will not work for iOS 6.

Alternatively, if your server can decide if new data is available and if the user needs to be alerted, traditional push notifications should suffice. In this case, you do not need to run the application in the background and check for new data (the server can do this and send a remote notification with alert / sound). This will obviously work on iOS 6 as well.

+7


source







All Articles