在 iOS10 的 UILocalNotification 中有什么新功能
你可以使用 UILocalNotification
,旧的 API 也适用于 iOS10,但我们最好使用 User Notifications 框架中的 API。还有一些新功能,你只能使用 iOS10 User Notifications 框架。
对于更多信息,这也发生在远程通知中: 此处 。
新功能:
- 现在,你可以使用 iOS 10 在应用程序处于前台时呈现警报,声音或增加徽章
- 现在,当用户点击(或滑动)操作按钮时,你可以在一个地方处理所有事件,即使应用程序已被杀死也是如此。
- 支持 3D 触摸而不是滑动手势。
- 现在,你只需一行代码即可删除特定的本地通知。
- 通过自定义 UI 支持 Rich Notification。
我们很容易将 UILocalNotification
API 转换为 iOS10 用户通知框架 API,它们非常相似。
我在这里写了一个 Demo 来展示如何同时使用新旧 API: iOS10AdaptationTips 。
例如,
使用 Swift 实现:
- 导入 UserNotifications
/// Notification become independent from UIKit
import UserNotifications
-
请求 localNotification 的授权
let center = UNUserNotificationCenter.current() center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in // Enable or disable features based on authorization. }
-
安排 localNotification
-
更新应用程序图标徽章编号
@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 实施:
- 导入 UserNotifications
// Notifications are independent from UIKit
#import <UserNotifications/UserNotifications.h>
-
请求 localNotification 的授权
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) { if (!error) { NSLog(@"request authorization succeeded!"); [self showAlert]; } }];
-
安排 localNotification
-
更新应用程序图标徽章编号
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 。
#更新
因未捕获的异常’NSInternalInconsistencyException’而终止应用程序,原因:‘重复时间间隔必须至少为 60’
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 60, repeats: true)