在我的SwiftUI应用程序(Xcode 15.0.1,测试iPhone操作系统: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
始终保持为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
这样的单独的类有什么错?
1条答案
按热度按时间bqucvtff1#
didRegisterForRemoteNotificationsWithDeviceToken
不是UNUserNotificationCenterDelegate
的一部分,它是UIApplicationDelegate
协议的一部分。您可以将其他函数移动到不同的类,但
didRegisterForRemoteNotificationsWithDeviceToken
和didFailToRegisterForRemoteNotifcationsWithError
需要保留在AppDelegate
中