我有这个来处理推送通知:
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = notification.request.content.userInfo
save(userInfo)
// Do something with userInfo based on whether we tapped the push notif whe the app is opened or in the background
completionHandler()
}
现在我想做的是实现一个逻辑,当应用程序打开(前台)或隐藏(背景)时点击推送通知时,该逻辑将有很大不同。我如何区分这两者?谢谢。
您需要做的就是查看
applicationState
的 UIApplication
属性。
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = notification.request.content.userInfo
save(userInfo)
if UIApplication.shared.applicationState == .active {
// App in foreground when user tapped notification
} else {
// App in background when user tapped notification
}
completionHandler()
}