iOS 9中的UILocalNotification和iOS 10中的UNMutableNotificationContent?

问题描述:

我需要提供向后兼容性(iOS 9)到一个项目。我想出了这个:iOS 9中的UILocalNotification和iOS 10中的UNMutableNotificationContent?

if #available(iOS 10.0, *) { 
     let content = UNMutableNotificationContent() 
    } else { 
     // Fallback on earlier versions 
    } 

我应该在回退中写什么?我是否需要创建本地通知实例?

+0

是的,我们需要编写适用于iOS 9的回退逻辑,因为在引入它们的操作系统版本之前不支持API。 –

这里本地通知写入是支持两个版本的一个小例子:

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]; 
} 

夫特版本:

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] 
} 
+0

通知没有在iOS 9设备中触发,我们是否也需要设置时区? – Dee

下面的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) 
+0

尽管通知没有在iOS 9设备中触发,但我确实设置了启动日期,我们是否也需要设置时区? notification.fireDate = NSDate(timeIntervalSinceNow:1.0)作为日期 – Dee

+0

不需要设置时区。交叉检查你的火狐 – kb920