在 iOS10 的 UILocalNotification 中有什麼新功能

你可以使用 UILocalNotification,舊的 API 也適用於 iOS10,但我們最好使用 User Notifications 框架中的 API。還有一些新功能,你只能使用 iOS10 User Notifications 框架。

對於更多資訊,這也發生在遠端通知中: 此處

新功能:

  1. 現在,你可以使用 iOS 10 在應用程式處於前臺時呈現警報,聲音或增加徽章
  2. 現在,當使用者點選(或滑動)操作按鈕時,你可以在一個地方處理所有事件,即使應用程式已被殺死也是如此。
  3. 支援 3D 觸控而不是滑動手勢。
  4. 現在,你只需一行程式碼即可刪除特定的本地通知。
  5. 通過自定義 UI 支援 Rich Notification。

我們很容易將 UILocalNotification API 轉換為 iOS10 使用者通知框架 API,它們非常相似。

我在這裡寫了一個 Demo 來展示如何同時使用新舊 API: iOS10AdaptationTips

例如,

使用 Swift 實現:

  1. 匯入 UserNotifications
    ///    Notification become independent from UIKit
    import UserNotifications
  1. 請求 localNotification 的授權

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

  3. 更新應用程式圖示徽章編號

    @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>
  1. 請求 localNotification 的授權

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

  3. 更新應用程式圖示徽章編號

    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)