Setting Reminders/Alarms in iOS8-iOS9 - ios

Main question: Should I be using local notifications, alarms, or reminders if I want attach due dates to tasks in my app? I want them to get a notification even if the app isn't running when the deadline arrives.
I found this tutorial on using UILocalNotification which it says can:
gives us the ability to cast notifications to a user without running the application
However, it was written six years ago and also states that this was introduced in iOS4. I know that a lot changes over 5 iOS versions.
I also read, alternatively, that I could use Event Kit. However, that seems more complicated than UILocalNotification.
Lastly, I could use probably take the current date/time and reminder date/time and create a timer to countdown.
So if I just want to attach due dates to tasks in my app (they don't need to show up in reminders or in the calendar), what's the best method and why?

I found following chunk of code here. This is also a good tutorial for how to create event in application.
var notification = UILocalNotification()
notification.alertBody = "Todo Item \"\(item.title)\" Is Overdue" // text that will be displayed in the notification
notification.alertAction = "open" // text that is displayed after "slide to..." on the lock screen - defaults to "slide to view"
notification.fireDate = item.deadline // todo item due date (when notification will be fired)
notification.soundName = UILocalNotificationDefaultSoundName // play default sound
notification.userInfo = ["UUID": item.UUID, ] // assign a unique identifier to the notification so that we can retrieve it later
notification.category = "TODO_CATEGORY"
UIApplication.sharedApplication().scheduleLocalNotification(notification)

Related

Creating multiple local notifications at once fails

I'm working with local notification on iOS, but I'm having some problems when iOS tries to create the notifications. Some of them are created and some won't.
After reviewing my code a lot, I found that it was failing at the point where it was creating the local notification.
UIApplication.sharedApplication().scheduleLocalNotification(notification)
The only reason I could think of was that creating the notifications inside a loop, around 50-60 notifications, was too much for iOS to process. I'm doing it this way because all the notifications have a different time and different day, and belong to different things.
This is my block to create the local notifications:
let createdUid = self.generateNotificationUUID()
// create a corresponding local notification
let notification = UILocalNotification()
/* Time and timezone settings */
notification.fireDate = self.buildTime()
notification.repeatInterval = NSCalendarUnit.WeekOfYear
notification.timeZone = NSTimeZone.systemTimeZone()
/* Information settings */
notification.alertBody = "Sector \(notificationData["sector"]!): located at \(notificationData["name"]!) closes in 15 min."
notification.alertAction = "Open"
/* Badge settings */
notification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
notification.soundName = "ring.caf"
notification.userInfo = ["UUID": createdUid, ]
/* Schedule the notification */
UIApplication.sharedApplication().scheduleLocalNotification(notification)
And all this code is inside a loop. The same loop before calling the notification creation, dynamically builds the notificationData array.
The notification data array contains the sector, the name, and the time and day.
Time and day is used to calculate the notification fire date.
Sector and Name are used for the alert body.
And all the four values are used to generate the UID (UUID).
If I put a print and remove the UIApplication.sharedApplication()... all the data looks good and what it needs to be.
I've tried, to solve the problem, using
dispatch_async(dispatch_get_main_queue()) {...}
and
dispatch_sync(dispatch_get_main_queue()) {...}
But with async I received the same result that I had without it, and using sync my screen freezes.
I kind of lost with this, I'm sure that my issues are because iOS doesn't process in time all the notification creation, but I dunno how to fix it.
I hope someone can help me, I'm using XCode 7.3.1 and Swift 2.2
From the documentation: https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/
An app can have only a limited number of scheduled notifications; the
system keeps the soonest-firing 64 notifications (with automatically
rescheduled notifications counting as a single notification) and
discards the rest.

Local Notification for Alarm Snooze

I am working on an alarm application and I am using local notification for that. Now I want to add snooze functionality to my alarm and alert show minimum distance 1 minute.
I am set Multiple alarm in my application so how can I managed it's?
UILocalNotification instances have a userInfo property of type NSDictionary. This can be used for storing information that you can later use for identifying what the notification was for.
For example, you could add the following code before firing the notification:
notification.userInfo = #{kAlarmIdentifier: alarm.identifier"};
where kAlarmIdentifier is a constant string used as a key. Then, when the notification is triggered, you can get the alarm that caused it with something like:
MYAlarm *alarm = [alarmManager getAlarmForIdentifier:notification.userInfo[kAlarmIdentifier]];

iOS UILocalNotification scheduled, but not fired

I'm making an application where I want local notifications. And something strange is happening. I schedule the notification:
static func setNotification(body: String, departure: Double, notification_info: Dictionary<String, String> )
{
let notification = UILocalNotification()
notification.alertBody = body
notification.fireDate = NSDate(timeIntervalSinceNow: departure)
notification.alertAction = "ShowDetails"
notification.userInfo = notification_info
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().scheduleLocalNotification(notification)
print("notification over: " + String(departure))
print("-------" + String( notification.fireDate ))
}
I print in how much seconds i am supposed to get an notification.
I go to background mode, and keep watching when I will get an notification. when the time has passed, I get no notification, even though I am sure I am in background mode. (within Xcode I look at the debug navigator > Energy Impact, and it is saying I am in background).
When I restart my phone, and run the application, it does show the notification. Everything works perfect. And then later, after some more testing and using the application, my notifications stop working again (even though the notifications are still scheduled. I am following all the scheduled notifications with:
UIApplication.sharedApplication().scheduledLocalNotifications
I am still new with Swift, and I have no idea why this is happening. It's making me crazy not knowing why my notification is not firing, even though it is scheduled and everything...
Could anyone help me? If you need more information, please ask.
Edit:
I did set the permissions
The notifications are set in the AppDelegate
The departure time definitely is right
I made a mistake by not adding an unique ID to the notification (notification.userInfo was not always unique), and overwrites the previous scheduled notification. Thats why it something does work and sometimes doesn't.
Fixed the problem by making it unique.
Thanks
Since iOS 8.0, You have to ask for permission for local notifications as remote notifications:
let notificationSettings = UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
See more details in Location Notifications

How can I Specify Relative Time in UILocalNotification Alert Body (i.e. "starting in X minutes")?

I'm creating a local notification like this:
NSDate *fireDate = // start date from model
NSString *startingInMinutes = #"5 minutes"; // also actually from model
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = [NSString stringWithFormat:#"Your event is starting in %#", startingInMinutes];
notification.fireDate = fireData;
notification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
After the notification is shown to the user, however, the "5 minutes" string is quickly outdated.
For example, if the user doesn't interact with the notification for 30 minutes, it still shows "Your event is starting in 5 minutes." This is obviously due to the fact that the "5 minutes" is a hard-coded string.
Is there any syntax to specify a relative time (i.e. "in X minutes") in the alert body of a UILocalNotification?
You can't do anything about that.
It's a static text.
See documentation
This isn't possible because the notification's payload is set at the time it is scheduled (UILocationNotification) or delivered to APNS (remove notification).
You have two choices:
Leave your message as is and hope that the user realizes that the content of the message reflects a frozen point in time and can mentally do the math using the notification's timestamp.
Adjust the text to be slightly ambiguous ("Your event is starting soon") or reference an absolute time ("Your event starts at 4:00pm").
WatchKit does include WKInterfaceTimer, which is an OS-controlled counter label. It was created to decrease the amount of communication needed between an Apple Watch and an iPhone just to update UI as the time changes. If this functionality is important to you in a notification context, I would encourage you to submit a bug report asking for the enhancement at https://bugreport.apple.com.

How to assign an action when a local notification is shown in Swift [duplicate]

This question already has an answer here:
Can a local notification trigger code in iOS8?
(1 answer)
Closed 8 years ago.
How do I assign an action or execute code when a local notification is shown (not just tapped) in Swift? Currently I create the notification like this
var localNotification:UILocalNotification = UILocalNotification()
localNotification.alertAction = "Golie Report"
localNotification.alertBody = "Did you do what you could to meet your goal today?"
localNotification.fireDate = notifyTime
localNotification.category = "INVITE_CATEGORY";
localNotification.repeatInterval = NSCalendarUnit.CalendarUnitDay
and register it in AppDelegate.swift
You can't. You cannot respond in any way when a local notification is shown. If the notification fires when your app is frontmost, you get an event (and the notification is not shown). But when the notification is merely shown, then you get nothing. Your app is not involved in any way. Your app is not even running. That is what a notification is: it is between the system and the user (and only optionally, since the user can turn off notifications, the device, and so on).

Resources