0

I hope that someone could help me to find the better solution that fit my problem. The scenario is the following: My app should periodically connect in background to some third-party end point and fetch some data. If there is something interesting the app should present a notification, so the user can open the app and see what is interesting.

I've already done in Android with Worker API. On boot, or when the app is launched for the first time, I create a periodically work (each 30min) that connect to the end-point, download data and show a notification, if any.

On iOS what should I do? First of all, there is no bootup/startup listener. Background fetch as I understand don't suit my problem, because reboot or app kill. So I'm not able to find the right solution of my problem, the last chance is to use push notifications:

In this case I should have a server (maybe a PHP hosting with Cron Jobs) that periodically send a push notification to all my clients (maybe with firebase or iOS SDK, what's the better solution?), when the app receive that "push", should connect to the end-point, download some data, and present a user notification only if any.

Is this possible? Is there some examples? I'm sorry if the question was already asked but answers are very old and I need to know if nowadays there is a better solution to my problem.

EDIT

Thanks to anyone who participated in the comments but @Paulw11 has right. No way to be guaranteed that the app will do the background work. So, the only solution is to do everything in my backend.

Users must Sign In to the app, register on my remote database which endpoint they would follow and use Cron Job (or something like that) to send simple push notifications. I'm very disappointed.. Anyway I'm still open to other suggestions, if someone would answer to my question

Philip
  • 771
  • 1
  • 9
  • 24
  • can't you use one of the app delegates callbacks: https://stackoverflow.com/questions/3712979/applicationwillenterforeground-vs-applicationdidbecomeactive-applicationwillre/28230014 – Jacob Jul 17 '19 at 15:44
  • There is also background fetch: https://developer.apple.com/documentation/uikit/app_and_environment/preparing_your_ui_to_run_in_the_background/updating_your_app_with_background_app_refresh and finally you can use notifications like `UIApplication.didBecomeActiveNotification` and `UIApplication.didEnterBackgroundNotification` But ideally push notifications should be sent by your backend so you don't have to poll every 30 minutes. It means more stress on your server and it means a delay of up to 30 minutes (which may or may not be alright in your case) – Jacob Jul 17 '19 at 15:47
  • I've already mentioned Background fetch in the question, it does not fit my problem because the app is not designed to be opened every day, or frequent. One possible scenario is the the app isn't opened for a week or more but still each hour, every day, should check the end-point for updates. It's the main feature and the reason it become popular on android, I need to preserve this behaviour. – Philip Jul 17 '19 at 15:52
  • The end-point that the app should check isn't mine, that's the problem! – Philip Jul 17 '19 at 15:53
  • Silent push notifications and UNNotificationServiceExtension is the best option – canister_exister Jul 17 '19 at 15:55
  • @canister_exister can a silent push notification trigger a network connection and show local notification? If yes could you share even just a simple example? I ask because I'm quite new in iOS so I would like to do things in the right way, expecially this feature, thank you – Philip Jul 17 '19 at 16:00
  • using UNNotificationServiceExtension you can fetch your API in background and if you need notify user you just show notification – canister_exister Jul 17 '19 at 16:04
  • 1
    https://developer.apple.com/documentation/usernotifications/unnotificationserviceextension https://code.tutsplus.com/tutorials/ios-10-notification-service-extensions--cms-27550 https://medium.com/@tsif/ios-10-rich-push-notifications-with-media-attachments-a54dc86586c2 – canister_exister Jul 17 '19 at 16:04
  • 1
    1. You need to find how to send silent push. 2. You need to find how to use UNNotificationServiceExtension. – canister_exister Jul 17 '19 at 16:07
  • you can create a backend service (a worker) that polls your 3rd party api and fetches anything important and sends notifications accordingly. At least this way the logic is the same on both iOS and Android. But I still don't understand why you can't use background app refresh (https://developer.apple.com/documentation/uikit/app_and_environment/preparing_your_ui_to_run_in_the_background/updating_your_app_with_background_app_refresh) coupled with UILocalNotification ( https://developer.apple.com/documentation/uikit/uilocalnotification). – Jacob Jul 17 '19 at 17:21
  • @Kubee I can't do backend worker, because there are more then 300 end points. Users in the app select what they needs and the app periodically check latest news. A backend worker will kill 3rd-party servers, and I can't do this ;D Background fetch instead does not work after device reboot, unless the user open the app each reboot, but I don't like this behaviour, It's not how the app should work to be useful to my users – Philip Jul 17 '19 at 20:39
  • You could use silent push to trigger your app to check a server, but they have the same limitations as background fetch; if the device has been restarted or he user kills your app then your app cannot receive silent push notifications until it is relaunched. A non-silent push (ie a push that actually contains the required content) will be displayed by iOS (assuming the user has granted notification permissions) regardless of whether your app is running. – Paulw11 Jul 17 '19 at 21:25
  • @Paulw11 can you confirm this with some official documentation? As far I understand even silent push notification should put the app in background state after a reboot, unless the user forced kill the app. Now the last scenario is not what I would care about, because it's a user decision. BTW if silent notifications wont work after reboot maybe a non-silent push notification (one a day) could ensure that the app will be always in background? – Philip Jul 18 '19 at 07:28
  • As far as I know only Pushkit pushes will relaunch an inactive app and they are only for voip apps – Paulw11 Jul 18 '19 at 07:41

0 Answers0