Методы AppDelegate, не вызываемые при использовании Firebase Cloud Messaging (FCM) - PullRequest
1 голос
/ 30 октября 2019

Поскольку я установил Firebase с FCM для уведомлений, большинство моих методов appDelegate больше не вызываются (за исключением, например, didFinishLaunchingWithOptions: applicationDidEnterBackground или applicationDidBecomeActive. Есть ли что-то еще, чтобы получить нормальное поведение 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 Ответ

1 голос
/ 30 октября 2019

Вау! Я нашел ошибку. Я использую Pod Firebase в своем приложении, и очевидно, что служба Firebase Cloud Messaging (FCM) быстро запускает делегат приложения при запуске. Мне пришлось положить этот ключ FirebaseAppDelegateProxyEnabled в NO в моем файле info.plist, чтобы исправить это.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...