ios - 没有 AppDelegate 的 SwiftUI 远程推送通知 - 有可能吗?

标签 ios swift push-notification swiftui apple-push-notifications

我已经在我的 SwiftUI 应用程序中实现了推送通知,一切似乎都运行良好。如您所知,没有 AppDelegate 但我们仍然可以使用 @UIApplicationDelegateAdaptor .
但是我不喜欢这种方法,我想知道是否有办法在没有 AppDelegate 的情况下实现它?

class AppDelegate: NSObject, UIApplicationDelegate {
    private var gcmMessageIDKey = "gcmMessageIDKey"
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        Messaging.messaging().delegate = self
        
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = self
        
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: {_, _ in })
        
        application.registerForRemoteNotifications()
        
        return true
    }
    
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
        // If you are receiving a notification message while your app is in the background,
        // this callback will not be fired till the user taps on the notification launching the application.
        // TODO: Handle data of notification
        
        // With swizzling disabled you must let Messaging know about the message, for Analytics
        // Messaging.messaging().appDidReceiveMessage(userInfo)
        
        // Print message ID.
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: \(messageID)")
        }
        
        // Print full message.
        print(userInfo)
    }
    
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        // If you are receiving a notification message while your app is in the background,
        // this callback will not be fired till the user taps on the notification launching the application.
        // TODO: Handle data of notification
        
        // With swizzling disabled you must let Messaging know about the message, for Analytics
        // Messaging.messaging().appDidReceiveMessage(userInfo)
        
        // Print message ID.
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: \(messageID)")
        }
        
        // Print full message.
        print(userInfo)
        
        completionHandler(UIBackgroundFetchResult.newData)
    }
    
}

extension AppDelegate : UNUserNotificationCenterDelegate {
    
    // Receive displayed notifications for iOS 10 devices.
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        let userInfo = notification.request.content.userInfo
        
        // With swizzling disabled you must let Messaging know about the message, for Analytics
        // Messaging.messaging().appDidReceiveMessage(userInfo)
        
        // Print message ID.
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: \(messageID)")
        }
        
        // Print full message.
        print(userInfo)
        
        // Change this to your preferred presentation option
        completionHandler([[.banner, .sound]])
    }
    
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo
        // Print message ID.
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: \(messageID)")
        }
        
        // With swizzling disabled you must let Messaging know about the message, for Analytics
        // Messaging.messaging().appDidReceiveMessage(userInfo)
        
        // Print full message.
        print(userInfo)
        
        completionHandler()
    }
}

extension AppDelegate: MessagingDelegate {
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
        print("Firebase registration token: \(String(describing: fcmToken))")
        
        let dataDict:[String: String] = ["token": fcmToken ?? ""]
        NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
        // TODO: If necessary send token to application server.
        // Note: This callback is fired at each app startup and whenever a new token is generated.
        
        // Add a new document in collection "cities"
        
        guard let registrationToken = fcmToken else { return }
        guard let uid = Auth.auth().currentUser?.uid else { return }
        
        let db = Firestore.firestore()
        
        db.collection("users").document(uid).updateData(["registrationToken": registrationToken]) { err in
            if let err = err {
                print("Error writing document: \(err)")
            }
        }
    }
}

最佳答案

是否this link回答你的问题?引用:

You can use @UIApplicationDelegateAdaptor to provide an AppDelegate:

class AppDelegate: NSObject, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        return true
    }
}
@main struct SampleApp: App {
    @UIApplicationDelegateAdaptor private var appDelegate: AppDelegate
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

我只是在研究如何做到这一点,所以你可能领先于我。但看起来名字可能暗示@UIApplicationDelegateAdaptor 允许您使用在上面的代码中生成的 AppDelegate。
This other link也许解释得更好。看起来很像在SwiftUI里面使用UIKit .

关于ios - 没有 AppDelegate 的 SwiftUI 远程推送通知 - 有可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65675780/

相关文章:

ios - 在 ios 中使用 ZBarReaderViewController 导致条形码不一致

iphone - Cocos2D中如何防止在特定区域(也是 Sprite )内检测到 Sprite ?

ios - 我的结构无法解码

php - MySQL 的 filemtime 替代方案

push-notification - 如何从头开始创建推送通知服务器

ios - 如何部分启用/禁用远程推送通知客户端?

ios - 如何在我的应用程序中使用 Safari 的输入文件选择器?

ios - 插入 Firestore 数据时图像重复

ios - 当超过 20 位的大整数时,为什么 Swift 5 String(Int) 会失败?

android - GCM 在前台推送消息更新 UI,在后台发布通知。如何实现?