无法在Swift 2中安排本地通知

时间:2015-09-20 12:38:19

标签: swift uilocalnotification

我最近更新到Xcode 7 beta,我遇到了调度UILocalNotifications的问题。如果有人能弄清楚他们为什么不起作用,我真的很感激一些帮助。不要担心像时间和日期这样的其他类,这些是非常自我解释的,我已经证实它们工作正常。

我的对象:

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)")
}
}

我的实例:

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)

1 个答案:

答案 0 :(得分:3)

在iOS 8及更高版本中,使用本地或远程通知的应用必须注册他们打算提供的通知类型。

夫特

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

    UIApplication.sharedApplication().registerUserNotificationSettings( mySettings);

目标-C

UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;

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

[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];