2017-02-21 2 views
1

J'ai besoin de rétrocompatibilité (iOS 9) avec un projet. Je suis venu avec ceci:UILocalNotification dans iOS 9 et UNMutableNotificationContent dans iOS 10?

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

Que devrais-je écrire dans Fallback? Ai-je besoin de créer une instance de notification locale?

+0

Oui, nous avons besoin pour écrire une logique de secours pour iOS 9 car les API ne sont pas supportées avant la version de l'OS dans lequel elles ont été introduites. –

Répondre

3

Voici un petit exemple sur le soutien à la fois la version:

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

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

n'a pas déclenché dans iOS 9 appareil, avons-nous besoin de définir le fuseau horaire aussi? – Dee

2

Ci-dessous iOS 10.0 pour écrire de notification locale ci-dessous le code

 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

J'ai défini la date de déclenchement mais la notification n'a pas été déclenchée sur l'appareil iOS 9, devons-nous également définir le fuseau horaire? notification.fireDate = NSDate (timeIntervalSinceNow: 1.0) en tant que date – Dee

+0

Aucune configuration de fuseau horaire n'est requise. Cross vérifier votre firedate notification – kb920