【发布时间】:2023-03-26 21:29:01
【问题描述】:
我有一个使用 Google 的 FCM 发送消息的服务器 (Java)。 在 Android 上它工作正常,我可以收到通知,但在 iOS 上由于某种原因它没有。
这是我的 AppDelegate.swift:
import UIKit
import Firebase
import FirebaseMessaging
import UserNotifications
import GoogleMobileAds
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
let gcmMessageIDKey = "my_message_type"
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
FirebaseConfiguration.shared.setLoggerLevel(.min)
GADMobileAds.sharedInstance().start(completionHandler: nil)
Messaging.messaging().delegate = self
registerToFirestoreMessaging(application)
return true
}
func registerToFirestoreMessaging(_ application: UIApplication) {
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: { _, _ in }
)
application.registerForRemoteNotifications()
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
Messaging.messaging().appDidReceiveMessage(userInfo)
print("User Info: \(userInfo)")
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
print("User Info: \(userInfo)")
Messaging.messaging().appDidReceiveMessage(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
func application(_ application: UIApplication,
didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Unable to register for remote notifications: \(error.localizedDescription)")
}
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("APNs token retrieved: \(deviceToken)")
#if PROD_BUILD
Messaging.messaging().setAPNSToken(deviceToken, type: .prod)
print("APNs prod token")
#else
Messaging.messaging().setAPNSToken(deviceToken, type: .sandbox)
print("APNs sandbox token")
#endif
}
}
@available(iOS 10, *)
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)
print("User Info: \(userInfo)")
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
completionHandler([[.alert, .sound]])
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
Messaging.messaging().appDidReceiveMessage(userInfo)
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)
}
}
我的能力中有后台模式下的推送通知和远程通知。 我尝试在启用和禁用 swizzling 的情况下使用此代码。它没有任何改变。
感谢阅读。欢迎任何帮助!
【问题讨论】:
-
嗨!您是否在 FCM 控制台上上传了您的 APNS 证书/Auth Token?
-
英文中的'revice'是什么意思?
-
这是大海捞针。是什么让您认为这是一个编码问题? “在 iOS 上它不是”是什么意思?怎么不行?
-
抱歉,revice 是一个错字。修好了。
-
我已将 APNS 密钥上传到 FCM 控制台。服务器和 FCM API 都声称消息已成功发送,但我的 iphone 上没有收到它......代码与 Google 的文档完全相同,我想我已经完成了所有步骤。我也用过这个raywenderlich.com/…
标签: swift google-cloud-firestore firebase-cloud-messaging