UILocalNotification在iOS10中已弃用

这可能是一个提前的问题,但我不知道如何使用UILocalNotification中的UILocalNotification。 我目前正在部署iOS8的应用程序,所以可以使用UILocalNotification吗?

是的,你可以使用UILocalNotification ,旧的API也适用于iOS10,但我们最好使用用户通知框架中的API。 还有一些新function,你只能使用iOS10的用户通知框架。

这也发生在远程通知,更多信息: 在这里 。

新function:

  1. 现在,您可以在iOS 10中显示警报,声音或增加徽章,同时应用程序也在前台
  2. 现在,当用户点击(或滑动)动作button时,即使应用程序已被杀死,您也可以在一个地方处理所有事件。
  3. 支持3D触摸,而不是滑动手势。
  4. 现在你可以通过一个行代码去除特定的本地通知。
  5. 支持丰富的通知自定义用户界面。

我们很容易将UILocalNotification API转换为iOS10用户通知框架API,它们非常相似。

我在这里写一个Demo来展示如何同时使用新的和旧的API: iOS10AdaptationTips

例如,

用Swift实现:

  1. 导入UserNotifications

     /// Notification become independent from UIKit import UserNotifications 
  2. 请求localNotification的授权

      let center = UNUserNotificationCenter.current() center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in // Enable or disable features based on authorization. } 
  3. 安排localNotification

  4. 更新应用程序图标徽章编号

     @IBAction func triggerNotification(){ let content = UNMutableNotificationContent() content.title = NSString.localizedUserNotificationString(forKey: "Elon said:", arguments: nil) content.body = NSString.localizedUserNotificationString(forKey: "Hello Tom!Get up, let's play with Jerry!", arguments: nil) content.sound = UNNotificationSound.default() content.badge = UIApplication.shared().applicationIconBadgeNumber + 1; content.categoryIdentifier = "com.elonchan.localNotification" // Deliver the notification in five seconds. let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 60.0, repeats: true) let request = UNNotificationRequest.init(identifier: "FiveSecond", content: content, trigger: trigger) // Schedule the notification. let center = UNUserNotificationCenter.current() center.add(request) } @IBAction func stopNotification(_ sender: AnyObject) { let center = UNUserNotificationCenter.current() center.removeAllPendingNotificationRequests() // or you can remove specifical notification: // center.removePendingNotificationRequests(withIdentifiers: ["FiveSecond"]) } 

Objective-C的实现:

  1. 导入UserNotifications

     // Notifications are independent from UIKit #import <UserNotifications/UserNotifications.h> 
  2. 请求localNotification的授权

     UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) { if (!error) { NSLog(@"request authorization succeeded!"); [self showAlert]; } }]; 
  3. 安排localNotification

  4. 更新应用程序图标徽章编号

     UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]; content.title = [NSString localizedUserNotificationStringForKey:@"Elon said:" arguments:nil]; content.body = [NSString localizedUserNotificationStringForKey:@"Hello Tom!Get up, let's play with Jerry!" arguments:nil]; content.sound = [UNNotificationSound defaultSound]; // 4. update application icon badge number content.badge = [NSNumber numberWithInteger:([UIApplication sharedApplication].applicationIconBadgeNumber + 1)]; // Deliver the notification in five seconds. UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5.f repeats:NO]; UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond" content:content trigger:trigger]; /// 3. schedule localNotification UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { if (!error) { NSLog(@"add NotificationRequest succeeded!"); } }]; 

去这里获取更多信息: iOS10AdaptationTips

更新

终止应用程序由于未捕获的exception“NSInternalInconsistencyException”,原因:'时间间隔必须至less60,如果重复'

 let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 60, repeats: true) 

苹果已经做了一遍,正确的实施是:AppDelegate.swift

 if #available(iOS 10.0, *) { let center = UNUserNotificationCenter.currentNotificationCenter() center.requestAuthorizationWithOptions([.Alert, .Sound]) { (granted, error) in // Enable or disable features based on authorization. } } else { // Fallback on earlier versions } 

不要忘记添加

 import UserNotifications 

Objetcive-C适用于iOS 10的本地通知

如果你正在编程一段时间,我相信你已经熟悉了UILocalNotification类,现在随着iOS 10的到来,你可以看到UILocalNotification已经被弃用了。 有关详细的实现,请访问此博客文章

https://medium.com/@jamesrochabrun/local-notifications-are-a-great-way-to-send-notifications-to-the-user-without-the-necessity-of-an-b3187e7176a3#.nxdsf6h2h