2017-02-21 4 views
1

Мне нужно вернуть обратную совместимость (iOS 9) к проекту. Я придумал это:UILocalNotification в iOS 9 и UNMutableNotificationContent в iOS 10?

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

Что я должен писать в Fallback? Мне нужно создать локальный экземпляр уведомления?

+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

Ниже прошивкой 10.0 для локального записи Notification ниже кода

 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) as Date – Dee

+0

Установка часового пояса не требуется. Перекрестная проверка вашего уведомления – kb920

Смежные вопросы