swift 用户通知点击

swift userNotification clicking

点击通知后如何处理?

adding action是指在通知中添加新按钮,我不想添加按钮;我想在选择通知时转到特殊视图控制器,例如 android 中的 builder.setContentIntent

我读了 Managing Your App’s Notification Support 但找不到任何东西。

对于 ios 10 或更高版本,有两种处理通知的新方法。

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)

This Method called when user tap on notification when app is in foreground.

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)

This Method called when app is in background.

并且对于 < ios 10

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)

This Method called when app is in background.(You won't able to see notification if app is in foreground but you can get notification in above method)

以及通知权限

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    if #available(iOS 10.0, *) {
        UNUserNotificationCenter.current().delegate = self
    }

    if !UIApplication.shared.isRegisteredForRemoteNotifications {

        if #available(iOS 10, *) {
            UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in }
            UIApplication.shared.registerForRemoteNotifications()
        }else if #available(iOS 9, *) {
            UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
            UIApplication.shared.registerForRemoteNotifications()
        }
    }
 return true
 }