1

Why doesn't it show a notification after the button has been pressed?

If the fireDate was NSDate(timeIntervalSinceNow: 10) it would work fine

How can i get notifications from an array?

    @IBAction func buttonPressed(sender: AnyObject) {

    var stringDate = dateFormatter.stringFromDate(timePicker.date)
    alarmArray.append(stringDate)

    let fixedAlarmArray = alarmArray
    NSUserDefaults.standardUserDefaults().setObject(fixedAlarmArray, forKey: "alarmArray")
    NSUserDefaults.standardUserDefaults().synchronize()


    //Local Notification
    var localNotification:UILocalNotification = UILocalNotification()
    localNotification.alertAction = "Go to app"
    localNotification.alertBody = "Its Time!"
    localNotification.fireDate = timePicker.date
    localNotification.timeZone = NSTimeZone.defaultTimeZone()
    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

    self.view.endEditing(true)

}
Amur
  • 31
  • 7

1 Answers1

0

The date specified in fireDate is interpreted according to the value of this property. If you specify nil (the default), the fire date is interpreted as an absolute GMT time, which is suitable for cases such as countdown timers. If you assign a valid NSTimeZone object to this property, the fire date is interpreted as a wall-clock time that is automatically adjusted when there are changes in time zones; an example suitable for this case is an an alarm clock.

localNotification.fireDate = timePicker.date
localNotification.timeZone = NSTimeZone.defaultTimeZone()
// the problem is here, you are setting the timezone after inputing your date

you should use localtimeZone and set it before setting the fireDate

localNotification.timeZone = NSTimeZone.localTimeZone()
localNotification.fireDate = timePicker.date

Note:

In iOS 8 and later, apps that use either local or remote notifications must register the types of notifications they intend to deliver. The system then gives the user the ability to limit the types of notifications your app displays. The system does not badge icons, display alert messages, or play alert sounds if any of these notification types are not enabled for your app, even if they are specified in the notification payload.

do like this:

UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationType.Alert, categories: nil))

create an extension to combine the time picked with 0 seconds as follow:

extension NSDate {

    var day:            Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitDay,           fromDate: self).day           }
    var month:          Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitMonth,         fromDate: self).month         }
    var year:           Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitYear,          fromDate: self).year          }
    var minute:         Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitMinute,        fromDate: self).minute }
    var hour:           Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitHour,          fromDate: self).hour   }

    var fireDate: NSDate {
        return NSCalendar.currentCalendar().dateWithEra(1, year: year, month: month, day: day, hour: hour, minute: minute, second: 0, nanosecond: 0)!
    }
}

then just use it as follow:

localNotification.fireDate = timePicker.date.fireDate
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • The notification types are already registered and the date picker mode is set to Time not date. It does work if the fireDate was set to a certain interval but doesn't when using a time picker. The question is how do i use it with the time picker correctly. – Amur Dec 11 '14 at 13:12
  • I have updated the answer. The problem is that you are setting the time zone after setting the fireDate. – Leo Dabus Dec 12 '14 at 05:10
  • It actually didn't make a difference, but i did realise that i do get notifications just not at the exact minute, seconds later depending on the second that i accessed the time picker it will start counting from that second. – Amur Dec 12 '14 at 19:16
  • It will pick the actual local time seconds for sure. You can combine the date picker time with a custom time if you need. – Leo Dabus Dec 12 '14 at 19:18
  • If the time was 10:15:30 PM when i accessed the time picker and i set the alarm for 10:16 pm, it will notify me at 10:16:30 PM. I want it to notify me as soon as it hits 10:16 PM not the time that i accessed the time picker. – Amur Dec 12 '14 at 19:22
  • I have updated my answer, it will probably solve your problem now. – Leo Dabus Dec 12 '14 at 19:45