使用 Firebase 云消息传递 (FCM) 时未调用 AppDelegate 方法

Posted

技术标签:

【中文标题】使用 Firebase 云消息传递 (FCM) 时未调用 AppDelegate 方法【英文标题】:AppDelegate methods not called when using Firebase Cloud Messaging (FCM) 【发布时间】:2019-10-30 14:04:21 【问题描述】:

自从我为通知安装了 Firebase 和 FCM,我的大多数 appDelegate 方法都不再被调用(例如 didFinishLaunchingWithOptionsapplicationDidEnterBackgroundapplicationDidBecomeActive 除外。 是否还有其他事情可以检索 AppDelegate 的正常行为?谢谢!


import UIKit
import Firebase

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate 

    var window: UIWindow?

// Still called, app launches UI normally.
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool 
         FirebaseOptions.defaultOptions()?.deepLinkURLScheme = Bundle.main.bundleIdentifier
        FirebaseApp.configure()        
        UNUserNotificationCenter.current().delegate = self
        Messaging.messaging().delegate = self
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
          options: authOptions,
          completionHandler: _, _ in )
        application.registerForRemoteNotifications()
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = LoadingViewController.instantiate()
        self.window?.makeKeyAndVisible()
        return true 
    

// Not called anymore 
    func applicationWillResignActive(_ application: UIApplication) 
        log.verbose("applicationWillResignActive") 
    

// Not called anymore   
    func applicationDidEnterBackground(_ application: UIApplication) 
        log.verbose("applicationDidEnterBackground")
        RideController.shared.prepareForBackground() 
    

// Not called anymore  
    func applicationWillEnterForeground(_ application: UIApplication) 
        log.verbose("applicationWillEnterForeground") 
    

// Not called anymore
    func applicationDidBecomeActive(_ application: UIApplication) 
        log.verbose("applicationDidBecomeActive")
        RideController.shared.prepareForForeground() // Not called anymore
        // Watch
        WatchController.shared.startSession() 
    

    func applicationWillTerminate(_ application: UIApplication) 
        log.verbose("applicationWillTerminate")
        RideController.shared.prepareForBackground()
    



/ MARK: - UNUserNotificationCenterDelegate methods
extension AppDelegate: UNUserNotificationCenterDelegate 

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) 
        Messaging.messaging().appDidReceiveMessage(notification.request.content.userInfo)
    


// MARK: - MessagingDelegate methods
extension AppDelegate: MessagingDelegate 

    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) 
        log.info("FCM registration token received = \(fcmToken)")
        let dataDict:[String: String] = ["token": fcmToken]
        NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
    

    func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) 
        log.info("didReceive message = \(remoteMessage)")
    


【问题讨论】:

链接您的应用委托 应用仍在启动,UI 工作正常,只是不再调用其他生命周期方法... 所以我明白了,是的,你设置正确,这真的很奇怪 【参考方案1】:

哇!我发现了这个错误。我在我的应用程序中使用 Pod Firebase,显然 Firebase 云消息传递服务 (FCM) 在启动时调整了应用程序委托。我必须在我的 info.plist 文件中将此密钥 FirebaseAppDelegateProxyEnabled 设置为 NO 才能修复它。

【讨论】:

嗨@matt,谢谢你的反馈,对不起我初学者的错误,但我现在明白了!我编辑了我的帖子以提及 Firebase,现在更好了吗?

以上是关于使用 Firebase 云消息传递 (FCM) 时未调用 AppDelegate 方法的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Nuxt.js 实现 Firebase 云消息传递 (FCM)

Android Firebase 云消息传递令牌 (FCM) 令牌太短/不完整

使用 Firebase 云消息传递 (FCM) 时未调用 AppDelegate 方法

Firebase Admin SDK、FCM 云消息传递

如何在 Firebase 云消息传递 (FCM) 中使用深度链接

如何向 FCM(Firebase 云消息传递)令牌的特定用户发送消息?