iOS 10 UserNotifications:覆盖默认操作

时间:2016-11-11 16:44:57

标签: ios swift usernotifications

我正在尝试使用iOS 10 UserNotifications框架做一些非常基本的事情:在选择默认操作时打开特定的ViewController。我已经完成了许多教程,但他们都专注于自定义类别和操作,并且他们也没有真正显示在确定操作后启动特定ViewController的代码(这只是留下作为注释或打印语句)。

我想做两件事之一,取决于可能的事情:

  1. 实例化并显示故事板的初始视图控制器的模态ViewController,或
  2. 显示初始的ViewController,但首先向其中注入一些信息,以便它可以使用此信息来确定要切换到下一个的ViewController。
  3. 我的AppDelegate将自己设置为UNUserNotificationCenterDelegate:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Make this object responsible for handling notification events.
        UNUserNotificationCenter.current().delegate = self
        return true
    }
    

    然后我想使用以下内容来覆盖选择的ViewController:

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    
        switch response.actionIdentifier {
        case UNNotificationDismissActionIdentifier:
            // Notification was dismissed. Do nothing.
            completionHandler()
        case UNNotificationDefaultActionIdentifier:
            // App opened from notification.
            // ??? Select ViewController to show based on information found in the notification ???
            completionHandler()
        default:
            completionHandler()
        }
    }
    

0 个答案:

没有答案