Firebase CloudMessaging不工作Swift 5
我正在我的项目中实施Firebase云消息传递解决方案。我遵循了所有要求的步骤,但在从Firebase控制台发送测试消息或发布新消息时,我仍然没有收到任何通知 这是我的密码:Firebase CloudMessaging不工作Swift 5,firebase,firebase-cloud-messaging,apple-push-notifications,swift4,swift5,Firebase,Firebase Cloud Messaging,Apple Push Notifications,Swift4,Swift5,我正在我的项目中实施Firebase云消息传递解决方案。我遵循了所有要求的步骤,但在从Firebase控制台发送测试消息或发布新消息时,我仍然没有收到任何通知 这是我的密码: import UIKit import FirebaseCore import FirebaseMessaging import UserNotifications @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate {
import UIKit
import FirebaseCore
import FirebaseMessaging
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
// Variables
var window: UIWindow?
let gcmMessageIDKey = "gcm.message_id"
// Functions
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
FirebaseAuth.login(email: FirebaseAuth.adminUser.email, password: FirebaseAuth.adminUser.password) {
(userAuthenticaed) in
if userAuthenticaed
{
FirebaseDatabase.initDatabase()
FirebaseStorage.initStorage()
FirebaseDatabase.getContactDetails()
FirebaseDatabase.getInventory()
FirebaseAuth.logout()
FirebaseAuth.user = nil
}
}
Messaging.messaging().delegate = self
self.registerForPushNotifications(application: application)
return true
}
func registerForPushNotifications(application: UIApplication) {
if #available(iOS 10.0, *) {
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()
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
print(userInfo)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
print(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)")
}
}
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
print(userInfo)
completionHandler([[.alert, .badge, .sound]])
}
}
extension AppDelegate : MessagingDelegate {
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
let dataDict:[String: String] = ["token": fcmToken]
NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
print(fcmToken)
}
}
我从“功能”菜单启用了推送通知和远程通知
我生成了一个Auth APNs密钥,并将其上传到Firebase控制台中的项目中
我的资源调配配置文件包括推送通知
我确实看到调用了didRegisterForRemoteNotificationsWithDeviceToken
和didReceiverRegistrationToken
,因此注册成功,但是没有消息到达
我错过了什么