Nio Pullus
Nio Pullus

Reputation: 55

Cannot schedule local notification in Swift 2

I recently updated to Xcode 7 beta and I am having an issue with scheduling UILocalNotifications. If anyone can figure out why they are not functional, I would really appreciate some help. Don't worry about those other classes like Time and Date, those are pretty self explanatory and I have verified that they work fine.

My object:

class NotificationWithTime {
   var UINotification = UILocalNotification()
   init(title: String,  day: Date, time: Time) {
        let formatter = NSDateFormatter()
        let dayday = day.day
        let dayMonth = day.month
        let dayYear = day.year
        let timeMinute = time.minute
        var timeHour = time.hour
    if time.tod == TOD.PM {
        timeHour! += 12
    }
    formatter.dateFormat = "yyyy-MM-dd HH:mm"
    formatter.timeZone = NSTimeZone(abbreviation: "EST")
    UINotification.alertAction = title
    UINotification.fireDate = formatter.dateFromString("\(dayYear)-\(dayMonth)-\(dayday) \(timeHour!):\(timeMinute!)")
    UIApplication.sharedApplication().scheduleLocalNotification(UINotification)
    //print("set a new notification for" + "\(dayMonth)-\(dayday)-\(dayYear) \(timeHour):\(timeMinute)")
}
}

My instance:

let time = ScheduleGenerator.refineTime(Time(t: TOD.PM, d: 20, h: 8, m: 12, s: 0))
    _ = NotificationWithTime(title: "chicke is good", day: ScheduleGenerator.refineDate(Date(m: 9, d: 19, y: 2015)), time: time)

Upvotes: 0

Views: 813

Answers (1)

Paul3k
Paul3k

Reputation: 165

In iOS 8 and later, apps that use either local or remote notifications must register the types of notifications they intend to deliver.

Swift

    let mySettings = UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories: nil);

    UIApplication.sharedApplication().registerUserNotificationSettings( mySettings);

Objective-C

UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;

UIUserNotificationSettings *mySettings =
[UIUserNotificationSettings settingsForTypes:types categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];

Upvotes: 3

Related Questions