【问题标题】:FCM iOS Push Notification cannot receive any notificationFCM iOS Push Notification 收不到任何通知
【发布时间】:2019-05-16 01:35:04
【问题描述】:

我目前正在处理通知,但未能从 Firebase FCM 获得任何通知。

podfile配置为:

target 'Project' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for Project
  pod 'Firebase/Core'
  pod 'Firebase/Messaging'
end

我已经从Background fetches 激活了Push NotificationsRemote Notifications,并且已经阅读了stackoverflow 中的另一个主题,该主题目前与here 中的主题相似

我想与您分享我的 AppDelegate 代码,但首先我不得不说 Google 的推送通知文档似乎有点混乱,因为这里有很多被覆盖的方法,每个教程都有不同的方式来完成接收通知。

我已经导入了这些

import Firebase
import FirebaseInstanceID
import UserNotifications
import FirebaseMessaging

然后是代表团

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate{
...
}

那么这里是 willFinishLaunchWithOptions 方法

func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        FirebaseApp.configure()
        Messaging.messaging().delegate = self



        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 })
            // For iOS 10 data message (sent via FCM
        } else {
            let settings: UIUserNotificationSettings =
                UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)

        }

        application.registerForRemoteNotifications()


        return true
    }

这里是Messaging 委托函数。

func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
        print("MEssage ->  \(remoteMessage.appData)")
    }

    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
        print("Firebase registration token: \(fcmToken)")
    }

该函数在用于设置 apns 令牌的 Firebase 文档中。

func application(application: UIApplication,
                     didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
        Messaging.messaging().apnsToken = deviceToken as Data
    }

我已经从 FCM 发送了数百条通知,它在服务器端成功发送,但是当我记录收到的消息时,没有任何收入数据。

如果你问我为什么要在 willFinish 函数中实现配置,文档中有这样的注释。

        For devices running iOS 10 and above, you must assign the
     UNUserNotificationCenter's delegate property and FIRMessaging's 
delegate property. For example, in an iOS app, assign it in the 
applicationWillFinishLaunchingWithOptions: or 
applicationDidFinishLaunchingWithOptions: method of the app delegate.

如果您能提供任何帮助,我将不胜感激,因为目前很难看出它有什么问题。

【问题讨论】:

  • 你是说没有收到通知,还是说收到了通知,但没有数据?
  • 感谢您的关注,由于没有收到日志,所以没有收到通知
  • 通知和消息是有区别的。消息是基于数据的。通知出现在屏幕上。它们仅在应用程序处于后台或关闭时出现。当应用在前台时,它们不会出现。
  • 是的,但我尝试了两种方式来查看当前发生的情况,即使在前台测试中,也没有书面消息。
  • @AakashDave 感谢您的回复,它现在运行良好。我将再次阅读文档。

标签: swift firebase apple-push-notifications firebase-cloud-messaging


【解决方案1】:
  1. 请检查您是否在项目功能中激活了推送通知

  2. 创建开发 APNs 证书并将其添加到 Firebase 控制台项目设置

  3. 在您的应用代理中

    import FirebaseMessaging
    import UserNotifications
    
    
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate, 
    UNUserNotificationCenterDelegate, MessagingDelegate {
    
    var window: UIWindow?
    
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
         // Override point for customization after application launch.
    
    
    
        //REMOTE NOTIFICATION
    
        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)
        }
    
        application.registerForRemoteNotifications()
    
        Messaging.messaging().delegate = self
    
        let token = Messaging.messaging().fcmToken
        print("FCM token: \(token ?? "")")
    
    
        //Added Code to display notification when app is in Foreground
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().delegate = self
        } else {
            // Fallback on earlier versions
        }
    
    
        return true
    }
    
    
    func application(_ application: UIApplication,
                 didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        Messaging.messaging().apnsToken = deviceToken as Data
    }
    
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
        // Print full message.
        print(userInfo)
    
    }
    
    // This method will be called when app received push notifications in foreground
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
    { completionHandler([UNNotificationPresentationOptions.alert,UNNotificationPresentationOptions.sound,UNNotificationPresentationOptions.badge])
    }
    
    
    // MARK:- Messaging Delegates
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
        InstanceID.instanceID().instanceID { (result, error) in
            if let error = error {
                print("Error fetching remote instange ID: \(error)")
            } else if let result = result {
                print("Remote instance ID token: \(result.token)")
            }
        }
    }
    
    
    func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
        print("received remote notification")
    }
    
    
    
    }
    

【讨论】:

    【解决方案2】:

    我正在使用 FirebaseMessaging v4.1.4 并启用了方法调配

    application.registerForRemoteNotifications() --> 首先

    Messaging.messaging().delegate = self --> 第二

    您必须在请求远程通知后设置消息代理。 (我注意到您在请求许可之前调用它)

    在我的情况下,因为我在 iOS 13 的 didRegisterForRemoteNotificationsWithDeviceToken 方法中没有收到 APNS 令牌之前调用了它,而生成了 firebase 令牌。

    可能是因为没有生成 APNS 并且 firebase 没有映射到 APNS 令牌,您可能没有收到任何通知。

    【讨论】:

    • 我有同样的问题,在 ios13 中没有调用 didRegisterForRemoteNotificationsWithDeviceToken。但它在 ios12 中工作正常。如果你解决了这个问题,请分享代码
    • 上述修复是我为解决 iOS 13 的问题所做的。你能在 didRegisterForRemoteNotificationsWithDeviceToken 方法中获取 APNS 令牌和 firebase 令牌吗?
    • 我的问题解决了。这是网络连接问题
    【解决方案3】:

    他们说 警告:要以这种方式使用 FCM 直接通道,您必须使用旧版 HTTP API 发送消息。 HTTP v1 API 对发送到 iOS 设备的所有消息使用 APN。 你需要把 Messaging.messaging().shouldEstablishDirectChannek = true

    【讨论】:

      【解决方案4】:

      我在 ios 13 设备上遇到了同样的问题。但它在 ios 13 以下的设备上运行良好。 问题是没有在 ios 13 设备上调用 didRegisterForRemoteNotificationsWithDeviceToken 方法。 经过一番研究,我发现这是由于网络连接问题。然后我从我的办公室wifi切换到蜂窝网络,之后它工作了。 didRegisterForRemoteNotificationsWithDeviceToken 返回了设备令牌。

      【讨论】:

      • 我关闭了蜂窝网络然后再打开它就可以了!谢谢!
      【解决方案5】:

      我回答了一个类似的问题here

      关键步骤是在 Capabilities 中启用“推送通知”。

      祝大家编码愉快!

      【讨论】:

        猜你喜欢
        • 2018-06-13
        • 2018-12-29
        • 2019-02-06
        • 2021-07-04
        • 2019-08-17
        • 1970-01-01
        • 2017-01-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多