Hat*_*nzō 8 push-notification firebase swift swiftui
因此,我正在使用 SwiftUI 2 和新的应用程序生命周期构建 iOS 应用程序,尝试实现 AppsDelegate 和 Firebase 推送通知
\n这是我的代码示例
\nimport SwiftUI\n\n@main\nstruct App: App {\n \n @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate\n \n var body: some Scene {\n WindowGroup {\n ContentView().environmentObject(AppSettings())\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n和应用程序委托
\nimport Firebase\nimport FirebaseMessaging\nimport UserNotifications\nimport Foundation\nimport UIKit\nclass AppDelegate: NSObject {\n \n let gcmMessageIDKey = "gcm.message_id"\n \n private func setupFirebase(application: UIApplication) {\n FirebaseApp.configure()\n\n if #available(iOS 10.0, *) {\n // For iOS 10 display notification (sent via APNS)\n UNUserNotificationCenter.current().delegate = self\n\n let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]\n UNUserNotificationCenter.current().requestAuthorization(\n options: authOptions,\n completionHandler: {_, _ in })\n } else {\n let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)\n application.registerUserNotificationSettings(settings)\n }\n Messaging.messaging().delegate = self\n application.registerForRemoteNotifications()\n }\n \n}\n\nextension AppDelegate: UIApplicationDelegate {\n \n func application(_ application: UIApplication,\n didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {\n setupFirebase(application: application)\n return true\n }\n \n}\n\nextension AppDelegate: MessagingDelegate {\n func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {\n print("Firebase registration token: \\(fcmToken)")\n }\n}\n\n@available(iOS 10, *)\nextension AppDelegate: UNUserNotificationCenterDelegate {\n \n func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {\n let userInfo = notification.request.content.userInfo\n \n // With swizzling disabled you must let Messaging know about the message, for Analytics\n // Messaging.messaging().appDidReceiveMessage(userInfo)\n \n // Print message ID.\n \n if let messageID = userInfo[gcmMessageIDKey] {\n print("Message ID: \\(messageID)")\n }\n \n // Print full message.\n // print(userInfo)\n \n completionHandler([.banner, .badge, .sound])\n }\n \n func userNotificationCenter(_ center: UNUserNotificationCenter,\n didReceive response: UNNotificationResponse,\n withCompletionHandler completionHandler: @escaping () -> Void) {\n let userInfo = response.notification.request.content.userInfo\n // Print message ID.\n \n if let messageID = userInfo[gcmMessageIDKey] {\n print("Message ID: \\(messageID)")\n }\n \n // Print full message.\n print(userInfo)\n \n completionHandler()\n }\n \n}\nRun Code Online (Sandbox Code Playgroud)\n但是当我\xe2\x80\x99m 测试来自 firebase 控制台的通知时,它没有出现。
\n当写这个答案时,Firebase 允许您禁用 swizzling,但还有几个其他区域必须更新才能使其工作。
Firebase 现已删除这些附加要求,您应该相应地更新您的实现。
SwiftUI 应用程序应使用 UIApplicationDelegateAdaptor 或 NSApplicationDelegateAdaptor 属性包装器来提供与适当的应用程序委托协议相对应的类型。
您应该仔细检查文档,因为 SO 上的帖子很容易就会过时。
我能够让你的代码工作。我认为 Firebase 在调整AppDelegate.
如果您通过将密钥添加FirebaseAppDelegateProxyEnabled到您的Info.plist、将其设为 aBoolean并将其设置为 来禁用混合NO。
然后,AppDelegate您需要添加以下内容,以便它向 Firebase 注册 APNS 令牌:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
}
Run Code Online (Sandbox Code Playgroud)
为了使 Firebase Cloud Messaging 正常工作,需要在 APNS 令牌与其令牌之间建立链接。如果未建立链接,则您将无法发送消息。
您可能还想设置一个消息传递委托,这允许您监视令牌刷新。这可以通过遵守消息传递委托协议来完成,更多信息可以在此处找到。
| 归档时间: |
|
| 查看次数: |
5032 次 |
| 最近记录: |