Background Fetch at Specific Time
Asked Answered
F

4

17

I am looking for solution to get data in background mode even app is terminated. There are lots of tutorials and answers available for this questions, but my questions is different than other. I haven't find any proper solution on stackoverflow, so posted this question.

I have scenario which I can explain. I'm using realm database which store event date, name, time, address etc. Now the thing is that, I want to write a code which are execute in background, In this code I want to get all event data and compare their date with today's date. And based on days remaining between these days fire local notification to remind user about how many days are remaining for specific event.

I want to call this background fetch method exactly 8 AM in local time everyday.

I haven't write any code due to confused with Background fetch and implementation. Can anyone know how to implement this ?

Help will be appreciated.

Franks answered 19/3, 2018 at 17:24 Comment(6)
In a very simple term, you cant schedule any task to download the data at a specific time in iOS, the best you could do is to opt for background capabilities but none can guarantee that your app gets a chance to download data at a specific time. You could also opt for silent notification but even that is equally unreliable as others are :)Thekla
Thanks for help. As you think about downloading data than it not right. I have data with local realm db.Franks
Implement silent push notificationEncyclopedia
Silent notifications are seriously unreliable. I had them working reliably while debugging my app, but in a release build it would be hours in-between silent pushes. I talked to Apple's tech support and their response was that there is no guarantee when a silent push will be delivered, or if it is delivered at all.Obfuscate
Generally a silent push will be delivered as reliably as a normal push. It certainly shouldn't take hours from when you send the push to Apple's servers, but if the app is terminated it won't be actioned by iOS.Alecto
@PaulMarshal you need to think differently about your problem. Either perform the comparisons on the server and send a normal push to the user with the reminder (taking the user's Timezone into account) or use background fetch and when it runs schedule a local notification for 8 o'clock. Each time the fetch runs you can update the pending notification if you have more/new data.Alecto
F
4

I have got solution to fix issue. As per I have talked,

I want to write a code which are execute in background, In this code I want to get all event data and compare their date with today's date. And based on days remaining between these days fire local notification to remind user about how many days are remaining for specific event.

So, when I getting response from API, I'm creating local notification for all events with unique id, which are fire and repeat daily at specific time, where I have display event time and remaining days of event.

Franks answered 5/6, 2019 at 10:39 Comment(2)
For some cases, that solution don't work properly. Think, if you get an event from your API at 10PM, and schedule a notification at the next 8AM saying that your event will occur at 11AM on the same day, but your event can be changed any time between 10PM(the time you get the response from your API) and 8AM(the time you send the notification), and you will show a wrong notification to the user.Leclair
@RomuloBM, You are right as per the way that display event notification. But my concern was to display reminder from before 5 days to event date on 8 AM, So that purpose it fins.Franks
C
3

Apple documentation states that there is no guarantee when background fetch is performed. It's up to the system to decide.

The system waits until network and power conditions are good, so you should be able to retrieve adequate amounts of data quickly.

The guaranteed way to wake your app is to send at 8 am VoIP push notification from the server. It is guaranteed that the app will be wakened upon receiving a push, and you'll be able to execute the jobs you need. For more details, https://medium.com/ios-expert-series-or-interview-series/voip-push-notifications-using-ios-pushkit-5bc4a8f4d587

Coterminous answered 17/5, 2019 at 7:6 Comment(1)
I have already read about this article. I don't need to VOIP Push NotificationFranks
M
1

It is not possible to wake up the app from suspended mode in iOS except with push notification (for this server has to send push notification).

Minorite answered 22/5, 2019 at 5:54 Comment(0)
H
0

This is something which is not documented anywhere. IMHO and experience with iOS, Apple must be recording user activities since the start of the iOS era. ScreenTime is a product of those recordings only that apple was able to create and visualize the data to present a user facing app that very beautifully restricts, manages and displays your activities. In WWDC 2018, it was even quoted that apple will even detect, if the user opens your app at let's say 9 PM daily before going to bed, iOS will allow every possible resource (battery, internet, processing time etc) to that app at 9 PM. But you need to understand your user activities before you do this. Let's take an example of:

News App: A majority of users would check the news in the morning (If they are not instagram addicts). At this time even apple should be biased to open your app with background fetch. Unless of-course there is too much of a resource crunch

A Game: Many games allow provide a restriction time by calling it "recharge" or "re-fill" the energy of a character before user can play another round. This is done so that the addicted person would buy gems to remove that restriction and hence monetize the idea. But if you know that the refill process is completed before 8:00 AM in the morning or user plays your game daily at 8:00 AM configure background fetch accordingly.

Background fetch works with interval rather than specific time.

// Fetch data once an hour.
   UIApplication.shared.setMinimumBackgroundFetchInterval(3600)

Lastly why don't you try silent push notifications? Based on your question, I think silent notification is enough to wake your app after a push notification from the server, download some data and do your calculation and provide a local notification.

Hardman answered 21/6, 2019 at 7:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.