swift userNotification click

时间:2018-01-16 08:04:14

标签: swift select click usernotifications

如何点击通知时如何处理?

添加操作意味着在通知中添加新按钮,我不想添加按钮;我想在选择通知时转到特殊视图控制器,如android中的builder.setContentIntent

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

1 个答案:

答案 0 :(得分:1)

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

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

当应用在前台时,当用户点击通知时调用此方法。

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

当app在后台时调用此方法。

< ios 10

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

当app处于后台时调用此方法。(如果app位于前台,您将无法看到通知,但您可以通过上述方法获得通知)

通知许可

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
 }