如何解决仅当应用程序已打开时才从通知中调用AppDelegate方法
我正在尝试通过推送通知将用户按到特定的视图控制器上。
我目前在我的AppDelegate
中有此密码:
func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound,.badge]) { granted,error in
if let error = error {
print(error.localizedDescription)
} else {
application.registerForRemoteNotifications()
}
}
UNUserNotificationCenter.current().delegate = self
return true
}
当我按通知并从后台打开应用程序时,这些都没有被调用:
func userNotificationCenter(_ center: UNUserNotificationCenter,willPresent notification: UNNotification,withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert,.badge])
}
func userNotificationCenter(_ center: UNUserNotificationCenter,didReceive response: UNNotificationResponse,withCompletionHandler completionHandler: @escaping () -> Void) {
}
func application(_ application: UIApplication,didReceiveRemoteNotification userInfo: [AnyHashable : Any],fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
}
解决方法
如果您的应用未在后台运行,您将像这样在didFinishLaunchingWithOptions
中处理通知
let notificationInfo = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [String : Any]
,如果您的应用程序在后台/前景中运行,则您将处理点击通知
func userNotificationCenter(_ center: UNUserNotificationCenter,didReceive response: UNNotificationResponse,withCompletionHandler completionHandler: @escaping () -> Void)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。