使用Firebase Cloud Messaging(FCM)时未调用AppDelegate方法

时间:2019-10-30 14:04:21

标签: ios firebase appdelegate ios13

由于我在FCM上安装了Firebase来进行通知,所以不再调用大多数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)")
    }
}

1 个答案:

答案 0 :(得分:1)

哇!我发现了错误。我在我的应用程序中使用Pod Firebase,显然Firebase Cloud Messaging服务(FCM)在启动时使应用程序代理陷入混乱。我必须在我的info.plist文件中将此密钥FirebaseAppDelegateProxyEnabled设置为NO才能解决此问题。