0

I am working on application in which I want to run the background service after every 12 hours even the application is suspended or any state just like Facebook. And in that service, it will hit the webService and get the data. And when the data is changed after 12 hours, I will get the notification that this item is added, and when I tap that notification, i will move to the specific screen.

I have read on many forums that when the application is in background state, it will take only 3-4 minutes to terminate the app, and after that time, no event will occur. But I have seen on Facebook, WhatsApp or any other social media app, even the app is in not running, or in suspended state, the app still gets the notification.

Please guide me what should I do.

Asad Ali
  • 55
  • 6
  • You can use background fetch but execution period is not defined and not under your app's control. Push notification is probably better – Paulw11 Sep 20 '16 at 10:01
  • But I want to get the data after every 12 hours even the app is suspended. – Asad Ali Sep 20 '16 at 10:03
  • The only way to guarantee every 12 hours is to push a notification from a server every 12 hours (technically this isn't guaranteed either since the device could be offline). Background fetch may work for you, but the time periods aren't exact – Paulw11 Sep 20 '16 at 10:04

1 Answers1

0

This is not how it works. The notifications you see from Facebook or WhatsApp apps are Push Notifications.

The app is not fetching information from the server. It's the server that "pushes" information to your user's phone.

Push notifications don't require your app to be active to work. And tapping on them will start your app and show the correct screen if handled correctly.


For running a task periodically, you can look into Background Fetch.
This works great, but iOS decides when to call it and can completely stop calling it (if the phone is on power saving or if your app is manually killed for example).

Keep in mind that in iOS, there is NO WAY to force the device to execute code if your app is suspended/killed. You might want to review your workflow.

Community
  • 1
  • 1
Quentin Hayot
  • 7,786
  • 6
  • 45
  • 62
  • I get it. So is there any other way that I can run the webService after every 12 hours? – Asad Ali Sep 20 '16 at 10:01
  • I have implemented Background fetch, but that will work only if the app is in background state. If the app is suspended from the memory, it stops the task. – Asad Ali Sep 20 '16 at 10:09
  • Background fetch will periodically call your code, even if the app is suspended. You can't control when though. And it won't happen if the app has been killed (by the system or the user). – Quentin Hayot Sep 26 '16 at 16:24
  • @QuentinHayot so you are saying that when app is suspended in background . i can not resume it with remote notification ? because Im trying to achieve the same . pls help. im tired of this – Ambesh Tiwari Aug 08 '22 at 18:47
  • @AmbeshTiwari if it's suspended, you can use background fetch to preload stuff and execute related simple code BUT the system will decide IF and WHEN it will run it (you have no guarantee it will run at all). If the app is killed (by the user or the system), nothing will run at all. – Quentin Hayot Aug 10 '22 at 06:06