Add life every N hours - ios

I want to implement mechanism that will be add one life to UserDefaults.standard.value(forKey: "lifes") every 4 hours even if app was closed and notify user about it.
About notifications i read articles, it will not be hard. But how increase life number value if i didn't be in game for 12 hours. I should get +3 lifes when back.

You get an event when the game is paused (UIApplicationWillResignActive) so you can write down in user defaults what time it was. You get an event when the game resumes (UIApplicationDidBecomeActive) so you can check to see what time it is and how many hours have elapsed since you became inactive.

This is not possible if the app is "closed".
The only way to achieve this behaviour is to use Push Notifications (pushed by your server, not the app).
See this and this Apple documentation.
My opinion is, that the user will delete your app, as she will get disturbed by too frequent notifications.

Related

Can IOS app do things even when it is not working at all (Killed/Non-running state)

I Did lots of BG tasks in the past, But Trying to get data when app is killed (Even when user is giving Always permission) seems not to work.
I wonder if there is a solution for that.
To make things clear, I am not talking at the moment when app change from Active or Background even to suspended mode. I'm talking about if the user is using the app and set permissions but then kills the app and after that i need every hour to get data from the user and send data to them.
Is there a way to do that?
Can a Today widget help me with that? Does a Today widget "lives" all the time and i can get that from it 24/7?
Whet is working is CLLocation manager. This is working even when the app is killed. But only when the user is changing a location.
I need that to work when the user is in the same place also.
Non of the other methods work. Not BGTask (I need every hour on the hour and not when apple decide to do things), Nor Silent Push Notification for some reason (Regular ones work, but i Don't want to bother the user with a push every hour just for getting and sending data).
Sorry there is no code to show as this is a very general question. But i think that is very important one to many people and can't find an answer for that.

Background Fetch at Specific Time

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.
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.
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
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).
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.

Get CMMotionActivityManager updated when app is terminated

Right now I'm developing an app that needs to know what the user is doing activity-wise (standing still, running, walking, ect.) all the time. I found that using CMMotionActivity might be a good way to do this.
Except for one thing: when the app is terminated, the CMMotionActivityManager stops running. First I tried to solve this by creating a cronjob to send remote silent notifications every n minutes and wake the app for motion data. I then did some research and found that silent notifications do not wake the app.
After some more research, I found that using Location and startMonitoringSignificantLocationChanges the app will be woken from a terminated state when the user moves 500 meters or more.
However, I believe I need more data then one point every 500 meters. Something more along the lines of one point every 5 minutes would be nice. But the sense I get is that startMonitoringSignificantLocationChanges won't update even if the user stands still for a whole day.
So my question in a nutshell is two parts:
Am I misunderstanding startMonitoringSignificantLocationChanges? Will it give me a notification every so often even if the user doesn't move?
If I don't misunderstand significant changes, is there any way to get Motion Updates every n minutes even when the app is terminated?
Thanks friends.

How to cancel all UILocalNotification, if the user got reminded x many times?

I have a situation with UILocalNotification. I know there is an answer how to cancel a uilocalnotification
My scenario is though:
I would like to remind the user to use the app on daily basis.
If the user doesn't use the app for three days, then I would like to increase the interval from daily to every three days.
If the user still doesn't come back after 6 days, I would like to stop all notifications.
Reading the document it says there is this one method that is called when the app is running: application:didReceiveLocalNotification:. But this is only hit if the app is running at the time of event. What if the user doesn't come back?
Is there anyway my app could cancel all events after a certain number of inactive days have passed? I don't want to keep pestering the user, if there is no interest after all.
The only way is to setup all local notifications when you go to background.
So you same notification several times with different date - each of them are single notification (non repeated).
after 1 day
after 2 days
after 3 days
after 6 days
Each time user opens you up you clean all of them with "cancelAllLocalNotifications" - each time he goes to background you create them all.

Local Notifications Control Time

I have a local notification in my app that goes off exactly 5 days since the app was last used. I have had a review saying that the sound is annoying when it goes off at midnight. Is there anyway that I can prevent the notification from playing if it is between a certain system time? I really want to keep this notification but I don't want the users to become annoyed with it going off at early hours (even if they were the ones who were on at that time 5 days prior)
Any help would be much appreciated

Resources