2017-02-21 89 views
1

我需要提供向後兼容性(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。 –

回答

3

這裏本地通知寫入是支持兩個版本的一個小例子:

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

2

下面的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