【问题标题】:didReceiveRemoteNotification not called in foreground firebase cloud messaging for iOS在 iOS 的前台 Firebase 云消息传递中未调用 didReceiveRemoteNotification
【发布时间】:2023-04-04 02:54:01
【问题描述】:

我尝试在我的应用上实现 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)
}

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

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

【问题讨论】:

  • 这个完成处理程序根本没有调用吗?
  • 声明 UNUserNotificationCenterDelegate 与否?

标签: ios swift firebase firebase-cloud-messaging


【解决方案1】:

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

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

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

 }

【讨论】:

    【解决方案2】:

    请实现以下方法在前台接收推送通知

    对于 iOS10、Swift3:

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) 
    {
        completionHandler([.alert, .badge, .sound])
    }
    

    对于iOS10、Swift 2.3:

    @available(iOS 10.0, *)
    func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void)
    {
        //Handle the notification
        completionHandler(
           [UNNotificationPresentationOptions.Alert,
            UNNotificationPresentationOptions.Sound,
            UNNotificationPresentationOptions.Badge])
    }
    

    试试这个:

    func application(application: UIApplication,  didReceiveRemoteNotification userInfo: [NSObject : AnyObject],  fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    
        print("Recived: \(userInfo)")
    
        completionHandler(.NewData)
    
    }
    

    【讨论】:

    • 我在哪里实现这个方法?顺便说一句,iOS 9 及以下版本怎么样?
    • 在 appdelegate 中实现这个。 iOS9及以下不需要此方法
    • 在 iOS9 中检查它是否正常工作。我认为它应该可以工作。
    • 不,不是AppDelegate,放到UNUserNotificationCenterDelegate
    • @SivajeeBattina 感谢您的回复。抱歉,我不需要通知显示。我只想处理通知消息,这就是为什么我想调用 didReceiveNotficaition 委托方法。
    【解决方案3】:

    如果您使用 IOS 10 的推送通知 Firebase。 请添加此代码。希望能帮助你解决这个问题:

    @available(iOS 10, *)
    extension AppDelegate : UNUserNotificationCenterDelegate {
    
        // Receive displayed notifications for iOS 10 devices.
        func userNotificationCenter(_ center: UNUserNotificationCenter,
                                    willPresent notification: UNNotification,
                                    withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
            let userInfo = notification.request.content.userInfo
            // Print message ID.
            if let messageID = userInfo[gcmMessageIDKey] {
                print("Message ID: \(messageID)")
            }
    
            // Print full message.
            print(userInfo)
            completionHandler([])
        }
    
        func userNotificationCenter(_ center: UNUserNotificationCenter,
                                    didReceive response: UNNotificationResponse,
                                    withCompletionHandler completionHandler: @escaping () -> Void) {
            let userInfo = response.notification.request.content.userInfo
            // Print message ID.
            if let messageID = userInfo[gcmMessageIDKey] {
                print("Message ID: \(messageID)")
            }
    
            // Print full message.
            print(userInfo)
            completionHandler()
        }
    }
    
    extension AppDelegate : FIRMessagingDelegate {
        // Receive data message on iOS 10 devices while app is in the foreground.
        func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {
            print(remoteMessage.appData)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-12
      • 2017-12-18
      • 2017-03-14
      • 2020-07-10
      • 1970-01-01
      • 2019-02-06
      • 2020-07-16
      • 1970-01-01
      相关资源
      最近更新 更多