当用户从后台或前台点击推送通知横幅时,如何处理不同的逻辑?

问题描述 投票:0回答:1

我有这个来处理推送通知:

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()
}

现在我想做的是实现一个逻辑,当应用程序打开(前台)或隐藏(背景)时点击推送通知时,该逻辑将有很大不同。我如何区分这两者?谢谢。

ios swift push-notification
1个回答
0
投票

您需要做的就是查看

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()
}
© www.soinside.com 2019 - 2024. All rights reserved.