【问题标题】:Swift & FCM : How to get push notification data when switching from Background to Foreground?Swift & FCM:从后台切换到前台时如何获取推送通知数据?
【发布时间】:2021-12-06 04:53:42
【问题描述】:

我正在 Swift 中配置推送通知。到目前为止,我有 3 个场景。

1 - 前台应用 在前台,我认为我所做的一切都是正确的,因为我确实收到了推送通知数据。

func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        
        print("userNotificationCenter willPresent")
        let content = notification.request.content
        
        UIApplication.shared.applicationIconBadgeNumber = 0
        UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
        
        completionHandler([.alert, .sound])
        
    }

2 - 用户点击推送通知横幅 这也很好用。

func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
        print("userNotificationCenter didReceive")
       defer {
           completionHandler()
       }
       guard response.actionIdentifier == UNNotificationDefaultActionIdentifier else {
           return
       }
        
        let content = response.notification.request.content
        
        
        UNUserNotificationCenter.current().removeAllDeliveredNotifications()
    }

3 - 应用在后台,然后用户进入应用 在这种情况下,推送通知会到达用户的手机。但是,他们没有点击推送通知本身,而是进入了应用程序。而且我无法从推送通知中获取任何信息

谁能帮忙配置第三种情况?谢谢。

【问题讨论】:

  • @Paulw11 你 100% 确定这一点吗?
  • 其实他们已经添加了一个新的API——developer.apple.com/documentation/usernotifications/…如果用户在你的应用返回前台之前清除了通知那么他们就丢失了,但是如果他们还在通知中心你可以得到他们这样。通知不能保证送达,您不应将其作为更新应用的唯一方式
  • 我听说它只在 ios 15 上可用。但我会检查一下。谢谢

标签: ios swift push-notification notifications firebase-cloud-messaging


【解决方案1】:

你需要考虑applicationState

UIApplication.State

//AppDelegate
func application(_ application: UIApplication,
                 didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    
    switch UIApplication.shared.applicationState {
    case .active:
        print("Received push message from APNs on Foreground")
    case .background:
        print("Received push message from APNs on Background")
    case .inactive:
        print("Received push message from APNs back to Foreground")
    }
}

当应用从后台转前台时,UIApplication.Stateinactive

inactive 是 '应用在前台运行但没有接收事件。'

因此,我认为实现您想要的行为的最佳方法是自己编写。

例如,

//AppDelegate
func application(_ application: UIApplication,
                 didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    
    switch UIApplication.shared.applicationState {
    case .active:
        print("Received push message from APNs on Foreground")
    case .background:
        print("Received push message from APNs on Background")
    case .inactive:
        print("Received push message from APNs back to Foreground")

        guard let nav = window?.rootViewController as? UINavigationController,
              let currentVC = nav.viewControllers.last else {return}

        if currentVC is 'youWantViewController' { //if you want ViewController, use notification post
            let name = Notification.Name(rawValue: K.Event.pushRequest)
            NotificationCenter.default.post(name: name, object: nil)
        } else { //move to you want ViewController
           let vc = 'yourViewController'()
           root.navigationController?.pushViewController(vc, animated: true)
        }
    }

    completionHandler(.newData)
}

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-09-27
    • 2021-09-08
    • 2021-11-27
    • 2019-06-26
    • 2017-10-27
    • 2013-12-27
    • 2018-05-01
    相关资源
    最近更新 更多