【发布时间】:2021-02-06 21:19:30
【问题描述】:
所以,我正在使用 SwiftUI 2 和新的应用程序生命周期构建 iOS 应用程序,尝试实现 AppsDelegate 和 Firebase 推送通知
这里是我的代码示例
import SwiftUI
@main
struct App: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
var body: some Scene {
WindowGroup {
ContentView().environmentObject(AppSettings())
}
}
}
和 AppDelegate
import Firebase
import FirebaseMessaging
import UserNotifications
import Foundation
import UIKit
class AppDelegate: NSObject {
let gcmMessageIDKey = "gcm.message_id"
private func setupFirebase(application: UIApplication) {
FirebaseApp.configure()
if #available(iOS 10.0, *) {
// 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 })
} else {
let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
Messaging.messaging().delegate = self
application.registerForRemoteNotifications()
}
}
extension AppDelegate: UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
setupFirebase(application: application)
return true
}
}
extension AppDelegate: MessagingDelegate {
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
}
}
@available(iOS 10, *)
extension AppDelegate: UNUserNotificationCenterDelegate {
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)
completionHandler([.banner, .badge, .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)")
}
// Print full message.
print(userInfo)
completionHandler()
}
}
但是当我测试来自 firebase 控制台的通知时,它没有出现。
【问题讨论】:
-
你是在设备上测试还是在模拟器上测试?由于模拟器无法接收通知。
-
可以在设备上测试,但您也可以在模拟器上测试推送通知
-
您只能在模拟器上测试 APNS 通知。不是来自 Firebase 的通知。此外,如果您正在为至少需要 iOS14 的 SwiftUI 构建,为什么您的代码要进行可用性检查以确保操作系统至少是 iOS10?看起来你那里有一些冗余代码。
-
AppDelegate来自我刚刚复制的 Firebase 示例项目,请注意这种方法适用于UIKit项目。只是我不知道 Firebase Cloud 消息在新应用程序生命周期中是如何工作的?
标签: swift firebase push-notification swiftui