我正在尝试在iOS应用程序上使用Firebase Cloud Messaging实现推送通知。
我的应用程序使用SwiftUI生命周期。
我遵循了Firebase documentation的官方文档,但是,就目前而言,我无法使它工作。我尝试了很多方法,但是没有一种方法能使它工作。有人看到了问题所在吗?
我在一真实的设备上测试,它要求我的通知的许可。但没有人到达...
我的AppDelegate类别:
import SwiftUI
import FirebaseCore
import GoogleMobileAds
import FirebaseMessaging
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
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)
}
application.registerForRemoteNotifications()
Messaging.messaging().delegate = self
GADMobileAds.sharedInstance().start(completionHandler: nil)
return true
}
}
extension AppDelegate: UNUserNotificationCenterDelegate{
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
Messaging.messaging().appDidReceiveMessage(userInfo)
// Change this to your preferred presentation option
completionHandler([[.alert, .sound]])
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
Messaging.messaging().appDidReceiveMessage(userInfo)
completionHandler()
}
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
Messaging.messaging().appDidReceiveMessage(userInfo)
completionHandler(.noData)
}
func application(application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
}
}
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.
}
}
@main
struct BochoGameApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
var body: some Scene {
WindowGroup {
SplashScreen()
}
}
}
我在我的苹果帐户上创建了一个密钥,并启用了苹果推送通知服务(APNs)服务。在我用它配置Firebase控制台后。
(我删除了图像的空字段)
我设置下一个功能
在信息桩上,我将FirebaseAppDelegateProxyEnabled设置为false
此外,我有一个选中了推送通知的应用程序ID
在我的个人资料中说我可以使用推送通知。
我在Xcode上设置了预置描述文件
有什么想法吗?任何帮助都将不胜感激
2条答案
按热度按时间j2cgzkjk1#
最后,我找到了一个解决方案。官方文档建议如果你禁用swizzling,或者你正在构建一个SwiftUI应用程序,就改变方法。我不知道他们为什么这么建议,因为如果你保留原来的方法,就可以......
我按照本教程学习,现在推送通知到达我的设备。Excellent tutorial
l2osamch2#
好了,现在我意识到这篇文章的方法也有效。你只需要编辑它。