I need a way to find what time it will be after the "unknown" amount of time for the ore to be done.
After you start the timer it needs to calculate when to send the local notification. I need a way so the app can be completely closed and still notify them.
Thanks
This is the error I get when I implement the "notificationsAllowed" code.
To calculate the endtime you can use
timerEndTime = NSDate(timeIntervalSinceNow:NSTimeInterval(oreWanted * 11))
So you get a NSDate
object. Now you can fire a UILocalNotification
at that calculated time:
Note: You need to ask for notification permissions when your app starts up. I have simplified this here with notificationsAllowed
and soundsAllowed
which are boolean variables in my AppDelegate.
let appDelegate = UIApplication.sharedApplication.delegate as! AppDelegate
if appDelegate.notificationsAllowed {
let notification = UILocalNotification()
notification.fireDate = timerEndTime
notification.timeZone = NSTimeZone.defaultTimeZone()
notification.alertBody = "Notification body text"
notification.alertTitle = "Notification alert title"
if appDelegate.soundsAllowed {
notification.soundName = UILocalNotificationDefaultSoundName
}
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
EDIT
Register the app for notifications in AppDelegate:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var notificationsAllowed = false
var notificationSoundAllowed = false
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let notificationSettings = UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
return true
}
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
if notificationSettings.types & UIUserNotificationType.Alert != nil {
self.notificationsAllowed = true
}
if notificationSettings.types & UIUserNotificationType.Sound != nil {
self.notificationSoundAllowed = true
}
}