当使用单独的类管理MessagingDelegate、UNUserNotificationCenterDelegate时,推送通知在SwiftUI应用程序中不起作用

6tqwzwtp  于 11个月前  发布在  Swift
关注(0)|答案(1)|浏览(135)

在我的SwiftUI应用程序(Xcode 15.0.1,测试iPhone操作系统:16.7)中,当使用单独的类来管理MessagingDelegateUNUserNotificationCenterDelegate时,Firebase的推送通知不起作用,但当我将所有内容都放在AppDelegate类中时,它可以工作。
我的AppDelegate看起来像这样:

class AppDelegate: NSObject, UIApplicationDelegate {
    
        let remoteNotificationManager = RemoteNotificationManager()
        func application(
            _ application: UIApplication,
            didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
        ) -> Bool {
            application.registerForRemoteNotifications()
            configureFirebase()
            remoteNotificationManager.configure()
            return true
        }
    }

字符串
我的RemoteNotificationManager类看起来像这样:

final class RemoteNotificationManager: NSObject {

    func configure() {
        Messaging.messaging().delegate = self
        UNUserNotificationCenter.current().delegate = self
    }
}

// MARK: - Firebase MessagingDelegate

extension RemoteNotificationManager: MessagingDelegate {

    // This fcmToken will be used for device specific notification from Firebase
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
        // TODO: Remove print in integration
        if let fcmToken = fcmToken {
            print("RemoteNotificationManager fcm from api: \(fcmToken)")
        }
    }
}

// MARK: - UNUserNotificationCenterDelegate

extension RemoteNotificationManager: UNUserNotificationCenterDelegate {

    func application(
        _ application: UIApplication,
        didFailToRegisterForRemoteNotificationsWithError error: Error
    ) {
        NSLog("Failed to register remote notification. %@", error.localizedDescription)
    }

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

    // This method is called when user taps on a notification
    func userNotificationCenter(
        _ center: UNUserNotificationCenter,
        didReceive response: UNNotificationResponse,
        withCompletionHandler completionHandler: @escaping () -> Void
    ) {
        let userInfo = response.notification.request.content.userInfo
        // TODO: Remove print, integrate
        print("RemoteNotificationManager didReceive: \(userInfo)")
        completionHandler()
    }

    // This method is called when user already in the app and notification arrived
    func userNotificationCenter(
        _ center: UNUserNotificationCenter,
        willPresent notification: UNNotification,
        withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
    ) {
        // TODO: Remove print, integrate completionHandler
        print("RemoteNotificationManager willPresent: \(notification.request.content.userInfo)")
        completionHandler(.banner)
    }
}


在上面的代码中不接收deviceTokenMessaging.messaging().apnsToken始终保持为nil。但是如果我删除了类RemoteNotificationManager,并将所有代码放在AppDelegate中,就像下面一样,它可以工作:

class AppDelegate: NSObject, UIApplicationDelegate {

    func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
    ) -> Bool {
        application.registerForRemoteNotifications()
        configureFirebase()
        configureRemoteNotification()
        return true
    }
}

extension AppDelegate {

    private func configureFirebase() {
        FirebaseApp.configure()
    }

    private func configureRemoteNotification() {
        Messaging.messaging().delegate = self
        UNUserNotificationCenter.current().delegate = self
    }
}

// MARK: - UNUserNotificationCenterDelegate

extension AppDelegate: UNUserNotificationCenterDelegate {

    func application(
        _ application: UIApplication,
        didFailToRegisterForRemoteNotificationsWithError error: Error
    ) {
        NSLog("Failed to register remote notification. %@", error.localizedDescription)
    }

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

    // This method is called when user taps on a notification
    func userNotificationCenter(
        _ center: UNUserNotificationCenter,
        didReceive response: UNNotificationResponse,
        withCompletionHandler completionHandler: @escaping () -> Void
    ) {
        let userInfo = response.notification.request.content.userInfo
        // TODO: Remove print, integrate
        print("RemoteNotificationManager didReceive: \(userInfo)")
        completionHandler()
    }

    // This method is called when user already in the app and notification arrived
    func userNotificationCenter(
        _ center: UNUserNotificationCenter,
        willPresent notification: UNNotification,
        withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
    ) {
        // TODO: Remove print, integrate completionHandler
        print("RemoteNotificationManager willPresent: \(notification.request.content.userInfo)")
        completionHandler(.banner)
    }
}

// MARK: - Firebase MessagingDelegate

extension AppDelegate: MessagingDelegate {

    // This fcmToken will be used for device specific notification from Firebase
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
        // TODO: Remove print in integration
        print("APNS TOKEN \(Messaging.messaging().apnsToken)")
        if let fcmToken = fcmToken {
            print("RemoteNotificationManager fcm from api token: \(fcmToken)")
        }
    }
}


我的问题是有一个像RemoteNotificationManager这样的单独的类有什么错?

bqucvtff

bqucvtff1#

didRegisterForRemoteNotificationsWithDeviceToken不是UNUserNotificationCenterDelegate的一部分,它是UIApplicationDelegate协议的一部分。
您可以将其他函数移动到不同的类,但didRegisterForRemoteNotificationsWithDeviceTokendidFailToRegisterForRemoteNotifcationsWithError需要保留在AppDelegate

相关问题