Sending multiple Local Notifications in a period of time - swift

I am building an app where the user puts in there working hours and working days. I need to be able to send the user a local notification every 20 minutes to update them on something. I am having trouble being able to add the exact date and times to scheduleLocalNotification. Would this be done by adding multiple fire dates? Or an array of dates?
Any input helpful.

It is good programming to only have one notification for one function at the time. So you raise a notification to fire at 14:00h.
In the fired selector you can create a new notification for 14:30h.
However when you want to have the notifications at scheduled periods you can use the repeat = YES for the Timer notification.

Related

How to update body of repeated local notification in iOs 10?

I created a local notification with Swift 3. The problem is that I want to change the body of local notification.
For example:
- There are 15 left issues.
20 minutes later:
- There are 10 left issues.
Can anybody help me, please?
After research I can almost confirm that it is not possible to update the repeated notification without app being foreground.
When you schedule a repeated push notification, the title and body is already set. In order to change the tile and body we will need to catch notification. However, according to this answer, notification service extension only works for remote notification but not local notification.
SO... How to achieve this using work arounds?
I can think 2 ways to do this.
Schedule approx 60 notification manually and preset the title and body. However, in your case, it is still not possible because you will never know how many issues left after 20 mins?? If you can pre-calculate, then you can use this solution. The disadvantage for this is it can only schedule for 64 local notifications and each time when push a local notification it creates a new delivered notification stay in the system which will spam user's notification center.
The second way is to use Notification Content Extension. NOTE: You can still not modify the title and body for the notification delivered. However, you can create a custom screen when user force touch the notification bar. Maybe the subtitle can be something like force touch to see how many issues left and implement a custom screen to show the number of issues when user force touch on it.
I am still experiment on this. There's one more option that I want to suggest is you can always do a remote push notification if issue number changes, which might be a better user experience in your case instead of pushing a repeated one every 20 mins.
Useful Reference
Here is a tutorial I found useful for Notification Content Extension.
To understand more, here is a link to WWDC of introducing notification service extension and notification content extension.
I believe you can use Notification Content Extension. You kinda have much more freedom since you are working with a view controller. I would use that!

How to make local notification fire only once?

I'm trying to set local notification in swift firing once at a definite date, but it seems there is no such option for repeat interval . By the default it triggers every 24 hours. Is there any way to submit a notification, which fires only once, so that user didn't have to even open the app or etc?
You have to set the repeatInterval of your UILocalNotification to 0:
If you assign a calendar unit such as weekly (NSCalendarUnitWeekOfYear) or yearly (NSCalendarUnitYear), the system reschedules the notification for delivery at the specified interval. Note that intervals of less than one minute are not supported.
The default value is 0, which means that the system fires the notification once and then discards it.
Regarding your second question: no! The user has to open the app at least once! Otherwise the app does not execute any of its code. Register the notification on first launch.

Is it possible to call past date UILocal Notification?

I have to added a local notification for next 30 days(June-01-2013 t0 June-30-2013).
For Example:
I have to change the date on the "settings" to June-15-2013, and then I have to change the date to June-05-2013. The local notification not called for the (June-01 to June-15). After June-16 is working fine.
My question is
1) Any restrictions for the UILocalNotification to fire the past dates?
2) Is it possible to fire the local notification for past dates?
Please help me. Thanks in advance.
Apple docs states that
If the specified value is nil or is a date in the past, the
notification is delivered immediately.
So if you are setting the fireDate of a local notification in the past the notification will be fired immediately.
Also your case I think is a special one because you are manually change the device date settings. The date settings may be changed when the time zone is changed but your case is different, you are not changing the time zone, you just change the date and I think the local notifications before (in the past) your new date are ignored.
Well, this is one of the situation i had faced when cam testing of robustness of the app. My app was also issuing notification for future dates. All goes well, if user does not change the date. But as part of testing, changing date to future and then past makes disables some notification which were suppose to appear.
The way i workaround this situation was responding to following call back in application Delegate
-(void)applicationSignificantTimeChange:(UIApplication *)application
Here i re-issued all the notifications as per required from the current date. That did the trick in my case. It's a heavy solution as all notifications has to be deleted and re-issued, but did the required for me.
There is one more case where you may required to re-Sync the notification. User may had also changed the date, when app is closed. For this, i made the application to reSync the notification everytime it launches. You can optimize this approach, by checking if there is significant change in date or not.

Avoiding Local Push notification to fire after changing time

In my app, I want to send a Local Push notification every 30 minutes. One way is to just configure local push notification and fire it. However there is a possibility that user can change his time and move forward 30 minutes. In this way a cheat can be done.
I want to configure my app so that notification only occur after 30 minutes. How can I do that. My app does communicate with server and can get its timestamp but I want to do things which don't use much server resources.
The only way I can think of to detect a user altering the system clock is as follows:
When app launches, ask your server the time and note the difference between that and [NSDate date]. Persist that as [NSNumber numberWithFloat:serverOffset];
Implement a method like - (BOOL)deviceClockChanged that asks the server again and compares to the persisted value. If the difference is greater than some small tolerance for clock drift + latency on the synch request, then you can conclude that the clock was changed. Do all this in UTC so it works independent of user travels between time zones.
Consider this: if the user wants badly enough to fool your app about the time in order to delay a notification, messing up the rest of his phone, maybe you ought to just let him edit the notification schedule.
I can supply code examples for points 1 and 2 if you want, and if you want I can supply some #"alert text" for point 3 that will make the user feel really guilty about editing his notifications.
My original answer here. If you choose to let user edit notifications, these methods will be key... UIApplication has a property:
NSArray *scheduledLocalNotifications;
and implements:
- (void)cancelLocalNotification:(UILocalNotification *)notification
So to change one, cancel it, then reschedule it.

Send notification each day with different information

i want to send a notification each day at the same time with different information. I got no problem with showing the alert when i want to. The problem is what to show. First i tried to schedule one alert every day with different information in them, but that seems really inefficient. Then i tried with
-(void)application(UIApplication*)app didReceiveLocalNotification:(UILocalNotification *)Notif{}
but found out that it runs when the application is running in foreground only.
Is there any way to provide an array or something like it with the alert and tell the alert to get its body from the array with different index for different days?
Or should i do it with some notification that can run when the app is in background mode and the alert show and make a new alert?
All information must be provided when you schedule a local notification, so no, a notification's body cannot be sourced from an array as you described. If users are likely to open your app often and you know notification content in advance, perhaps you could ensure that x notifications are always scheduled (could be achieved by inspecting UIApplication's scheduledLocalNotifications).
This solution isn't ideal though, as it relies on users opening your app on a (somewhat) regular basis. I think push notifications are probably your best bet, but that requires access to a server, etc.
Yeah push could prob be the solution. But i dont have a server for that. The users will most likeley not open it any more after they have opened it the first time to activite the local-messages. So then this isnt really possible to do without push. when i tried a for-loop to schedule 250 days with different text each day it took about 2 minutes on iphone 4 so its not duable.