【发布时间】:2018-01-16 08:04:14
【问题描述】:
点击通知时如何处理?
添加动作意味着向通知添加新按钮,我不想添加按钮;当在android中选择builder.setContentIntent之类的通知时,我想去特殊的视图控制器。
我读过 Managing Your App’s Notification Support 但找不到任何东西。
【问题讨论】:
标签: swift select click usernotifications
点击通知时如何处理?
添加动作意味着向通知添加新按钮,我不想添加按钮;当在android中选择builder.setContentIntent之类的通知时,我想去特殊的视图控制器。
我读过 Managing Your App’s Notification Support 但找不到任何东西。
【问题讨论】:
标签: swift select click usernotifications
对于 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)
当应用程序在后台时调用此方法。
对于
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
当应用程序在后台时调用此方法。(如果应用程序在前台,您将无法看到通知,但您可以在上述方法中收到通知)
对于通知权限
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
}
【讨论】: