ios - didReceiveRemoteNotification 未在 iOS 的前台 firebase 云消息传递中调用

标签 ios swift firebase firebase-cloud-messaging

我尝试在我的应用程序上实现 Firebase 云消息传递。

我使用以下代码注册通知

  if #available(iOS 10.0, *) {
            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.current().delegate = self

            let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
            UNUserNotificationCenter.current().requestAuthorization(
                options: authOptions,
                completionHandler: {_, _ in })
        } else {
            let settings: UIUserNotificationSettings =
                UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }

当应用程序在后台时一切正常,但当应用程序在前台时,根本不会调用 didReceiveRemoteNotification。

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    // Print message ID.
    print("please help")
    if let messageID = userInfo[gcmMessageIDKey] {
        print("Message ID: \(messageID)")
    }

    // Print full message.
    print(userInfo)
    completionHandler(.newData)
}

有人知道这可能会出错吗?谢谢!

编辑:当应用程序在前台时,我不需要看到通知显示,我只想处理来自通知的消息

最佳答案

iOS 10.x 有一个在前台处理推送通知的新功能,你应该实现这个:

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    completionHandler(
        [UNNotificationPresentationOptions.alert,
         UNNotificationPresentationOptions.sound,
         UNNotificationPresentationOptions.badge])

 }

关于ios - didReceiveRemoteNotification 未在 iOS 的前台 firebase 云消息传递中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45139625/

相关文章:

ios - 是否可以使用 MKLocalSearch 获取 "restaurant"之类的位置类型

ios - 使用动画从按钮位置放大和缩小 UIView

iphone - IOS 在 AppDelegate 中访问模态视图

firebase - 如何在firestore集合中生成并保证独特的值(value)?

java - Android - 事务 - 任务尚未完成

ios - [iOS][ReactNative v0.19.0] 自定义 View 出现意外的黑色背景

xcode - Swift 编译器挂起!这是一个错误吗?

swift - 使用选择器时如何处理回调?

ios - Swift 强委托(delegate)引用

javascript - Firebase 部署在 Node js 上