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

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

在我的 SwiftUI 应用程序(Xcode 15.0.1,测试 iPhone OS:16.7)中,当使用单独的类来管理

MessagingDelegate
UNUserNotificationCenterDelegate
时,来自 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)
    }
}

在上面的代码中,不会收到

deviceToken
并且
Messaging.messaging().apnsToken
始终保持为零。但是如果我删除了 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
这样的单独课程有什么问题吗?

ios swift firebase swiftui push-notification
1个回答
0
投票

didRegisterForRemoteNotificationsWithDeviceToken
不是
UNUserNotificationCenterDelegate
的一部分,它是
UIApplicationDelegate
协议的一部分。

您可以将其他功能移至不同的类,但

didRegisterForRemoteNotificationsWithDeviceToken
didFailToRegisterForRemoteNotifcationsWithError
需要保留在您的
AppDelegate

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