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

问题描述 投票:0回答:1

由于我在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)")
    }
}

ios firebase appdelegate ios13
1个回答
0
投票

哇!我发现了错误。我在应用程序中使用Pod Firebase,并且显然Firebase Cloud Messaging服务(FCM)在启动时使应用程序委托蒙上了阴影。我必须在我的info.plist文件中将此键FirebaseAppDelegateProxyEnabled设置为NO才能对其进行修复。

© www.soinside.com 2019 - 2024. All rights reserved.