简体   繁体   English

在iOS的前台Firebase云消息传递中未调用didReceiveRemoteNotification

[英]didReceiveRemoteNotification not called in foreground firebase cloud messaging for iOS

I tried to implement firebase cloud messaging on my app. 我试图在我的应用程序上实现Firebase云消息传递。

I register the notification using the following codes 我使用以下代码注册通知

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

Everything works fine when the app is in the back ground, but when the app is in the foreground, didReceiveRemoteNotification is not called at all. 当应用程序位于后台时,一切正常,但是当应用程序位于前台时,根本不会调用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)
}

Anyone knows possible error with this? 有人知道这个错误吗? Thanks! 谢谢!

EDIT: I do not need to see a notification display when the app is in foreground, I merely want to handle the message from the notification 编辑:当应用程序处于前台时,我不需要看到通知显示,我只想处理通知中的消息

iOS 10.x has a new function that handles push notification when in foreground, you should implement this one: iOS 10.x具有一项新功能,可以在前台处理推送通知,您应该实现以下功能:

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

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

 }

Please implement below method to receive push notifications in foreground 请实施以下方法以在前台接收推送通知

For iOS10, Swift3: 对于iOS10,Swift3:

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

For iOS10, Swift 2.3: 对于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])
}

Try this once: 尝试一次:

func application(application: UIApplication,  didReceiveRemoteNotification userInfo: [NSObject : AnyObject],  fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {

    print("Recived: \(userInfo)")

    completionHandler(.NewData)

}

If you use Push Notification Firebase for IOS 10. Please add this code. 如果您将推送通知Firebase用于IOS 10,请添加此代码。 Hope to help you solve this problem : 希望能帮助您解决这个问题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS上的Firebase Cloud Messaging:改善前台通知的消息延迟 - Firebase Cloud Messaging on iOS: Improving message delay for foreground notifications iOS 设备上未显示 Firebase 云消息传递通知(前台和后台) - Firebase Cloud Messaging notifications not being displayed on iOS device (foreground and background) iOS推送通知-didReceiveRemoteNotification:fetchCompletionHandler:从不在前台调用 - iOS push notifications - didReceiveRemoteNotification:fetchCompletionHandler: never called in foreground 在Firebase中未调用didReceiveRemoteNotification吗? - didReceiveRemoteNotification not called in Firebase? Firebase Cloud Messaging无法在IOS上运行 - Firebase Cloud Messaging not working on IOS Firebase 云消息传递 - iOS 徽章 - Firebase Cloud Messaging - iOS Badges iOS中的Firebase Cloud Messaging有问题吗? - Issue with Firebase Cloud Messaging in iOS? IOS Firebase 云消息“InvalidApnsCredential” - IOS Firebase Cloud Messaging "InvalidApnsCredential" didReceiveRemoteNotification 未调用,iOS 10 - didReceiveRemoteNotification not called, iOS 10 为什么didReceiveRemoteNotification没有被调用但是didReceiveRemoteNotification:当我的应用程序在前台时调用fetchCompletionHandler? - Why is didReceiveRemoteNotification not called but didReceiveRemoteNotification:fetchCompletionHandler called when my app is in the foreground?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM