iOS 9 中的 UILocalNotification 和 iOS 10 中的 UNMutableNotificationContent?
UILocalNotification in iOS 9 and UNMutableNotificationContent in iOS 10?
我需要为项目提供向后兼容性 (iOS 9)。我想到了这个:
if #available(iOS 10.0, *) {
let content = UNMutableNotificationContent()
} else {
// Fallback on earlier versions
}
Fallback 应该写什么?我需要创建本地通知实例吗?
低于 iOS 10.0 用于本地通知写下面的代码
let notification = UILocalNotification()
let dict:NSDictionary = ["key" : "value"]
notification.userInfo = dict as! [String : String]
notification.alertBody = "\(title)"
notification.alertAction = "OK"
notification.fireDate = dateToFire
notification.repeatInterval = .Day
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().scheduleLocalNotification(notification)
这里有一个支持两个版本的小例子:
Objective-c版本:
if #available(iOS 10.0, *) {
UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
objNotificationContent.body = @"Notifications";
objNotificationContent.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"identifier" content:objNotificationContent trigger:trigger];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
}
else {
}
}];
}
else
{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = [[NSDate date] dateByAddingTimeIntervalInterval:60];
localNotif.alertBody = @"Notifications";
localNotif.repeatInterval = NSCalendarUnitMinute;
localNotif.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
Swift版本:
if #available(iOS 10.0, *) {
let content = UNMutableNotificationContent()
content.categoryIdentifier = "awesomeNotification"
content.title = "Notification"
content.body = "Body"
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: false)
let request = UNNotificationRequest(identifier: "FiveSecond", content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request) { (error) in
}
}
else
{
let notification = UILocalNotification()
notification.alertBody = "Notification"
notification.fireDate = NSDate(timeIntervalSinceNow:60)
notification.repeatInterval = NSCalendarUnit.Minute
UIApplication.sharedApplication().cancelAllLocalNotifications()
UIApplication.sharedApplication().scheduledLocalNotifications = [notification]
}
我需要为项目提供向后兼容性 (iOS 9)。我想到了这个:
if #available(iOS 10.0, *) {
let content = UNMutableNotificationContent()
} else {
// Fallback on earlier versions
}
Fallback 应该写什么?我需要创建本地通知实例吗?
低于 iOS 10.0 用于本地通知写下面的代码
let notification = UILocalNotification()
let dict:NSDictionary = ["key" : "value"]
notification.userInfo = dict as! [String : String]
notification.alertBody = "\(title)"
notification.alertAction = "OK"
notification.fireDate = dateToFire
notification.repeatInterval = .Day
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().scheduleLocalNotification(notification)
这里有一个支持两个版本的小例子:
Objective-c版本:
if #available(iOS 10.0, *) {
UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
objNotificationContent.body = @"Notifications";
objNotificationContent.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"identifier" content:objNotificationContent trigger:trigger];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
}
else {
}
}];
}
else
{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = [[NSDate date] dateByAddingTimeIntervalInterval:60];
localNotif.alertBody = @"Notifications";
localNotif.repeatInterval = NSCalendarUnitMinute;
localNotif.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
Swift版本:
if #available(iOS 10.0, *) {
let content = UNMutableNotificationContent()
content.categoryIdentifier = "awesomeNotification"
content.title = "Notification"
content.body = "Body"
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: false)
let request = UNNotificationRequest(identifier: "FiveSecond", content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request) { (error) in
}
}
else
{
let notification = UILocalNotification()
notification.alertBody = "Notification"
notification.fireDate = NSDate(timeIntervalSinceNow:60)
notification.repeatInterval = NSCalendarUnit.Minute
UIApplication.sharedApplication().cancelAllLocalNotifications()
UIApplication.sharedApplication().scheduledLocalNotifications = [notification]
}