无法在 Swift 2 内安排本地通知
Cannot schedule local notification in Swift 2
我最近更新到 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)
在 iOS 8 及更高版本中,使用本地或远程通知的应用程序必须注册它们打算传送的通知类型。
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];
我最近更新到 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)
在 iOS 8 及更高版本中,使用本地或远程通知的应用程序必须注册它们打算传送的通知类型。
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];