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中写什么? 我是否需要创建本地通知实例?
I need to give backward compatibility (iOS 9) to a project. I came up with this:
if #available(iOS 10.0, *) { let content = UNMutableNotificationContent() } else { // Fallback on earlier versions }What should I write in Fallback? Do I need to create a local notification instance?
最满意答案
这是一个支持两个版本的小例子:
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] }Here is a small example on supporting both version:
Objective-c version:
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 version:
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] }更多推荐
发布评论