使用.p8文件后未收到iOS Firebase云消息传递通知

问题描述 投票:0回答:3

我按照以下所有步骤在App Delegate中添加了相应的导入和代码。我还确保在运行应用程序时允许接受通知。

按照以下步骤操作,为什么我从Firebase云消息传递控制台发送通知后无法收到通知?

  1. 在我的开发者帐户中,我去了Certificates, Identifiers & Profiles
  2. Keys下,我选择了All并点击了右上角的Add按钮(+)
  3. Key Description下,我为签名密钥输入了一个唯一的名称
  4. 在qazxsw poi下,我选择了qazxsw poi复选框,然后点击qazxsw poi然后点击Key Services
  5. 我复制了APNs(在步骤7中使用)并点击Continue生成并下载Confirm密钥
  6. 我去了Key ID,点击了Download> .p8> Firebase
  7. Gear Icon> Project Settings下我去了第一部分Cloud Messaging(不是APNs证书),选择了iOS app configuration并上传了APNs Authentication Key键,APNs Authentication Key和我的Upload
  8. 在我的Xcode项目中,我去了.p8> Key ID,把它变成了Team Id,并检查了Capabilities
  9. 虽然仍然在Background Modes> On,并把它变成Remote Notifications
  10. 在AppDelegate中,我添加了CapabilitiesPush NotificationsOn,注册了import UserNotifications并添加了以下代码。

我在下面的所有打印声明中添加了断点,在我从Firebase发送消息后,没有一个被击中。

import FirebaseMessaging

消息会成功发送,如下图所示,但我从未收到过。

import Firebase

ios swift firebase push-notification firebase-cloud-messaging
3个回答
1
投票

我认为您还应该将此添加到您的代码中,否则您将不会收到推送通知。 Firebase现在需要apns令牌向您发送推送。

UNUserNotificationCenterDelegate

0
投票

好像你错过了一些FCM正常工作的配置。

从我看到你没有将令牌发送到firebase并且你没有注册FCM。

有关更多详细信息,请参阅import UserNotifications import FirebaseMessaging import Firebase class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { FirebaseApp.configure() UNUserNotificationCenter.current().delegate = self if #available(iOS 10.0, *) { UNUserNotificationCenter.current().requestAuthorization(options: [.sound,.alert,.badge]) { [weak self] (granted, error) in if let error = error { print(error.localizedDescription) return } print("Success") } application.registerForRemoteNotifications() } else { let notificationTypes: UIUserNotificationType = [.alert, .sound, .badge] let notificationSettings = UIUserNotificationSettings(types: notificationTypes, categories: nil) application.registerForRemoteNotifications() application.registerUserNotificationSettings(notificationSettings) } } // MARK:- UNUserNotificationCenter Delegates func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { Messaging.messaging().setAPNSToken(deviceToken, type: MessagingAPNSTokenType.unknown) var token = "" for i in 0..<deviceToken.count{ token = token + String(format: "%02.2hhx", arguments: [deviceToken[i]]) } print("Registration Succeded! Token: \(token)") } func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { print("Notifcation Registration Failed: \(error.localizedDescription)") } func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { if let gcm_message_id = userInfo["gcm_message_id"]{ print("MessageID: \(gcm_message_id)") } print(userInfo) } @available(iOS 10.0, *) func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { completionHandler(.alert) print("Handle push from foreground \(notification.request.content.userInfo)") let dict = notification.request.content.userInfo["aps"] as! NSDictionary let d = dict["alert"] as! [String:Any] let title = d["title"] as! String let body = d["body"] as! String print("Title:\(title) + Body:\(body)") showFirebaseNotificationAlertFromAppDelegate(title: title, message: body, window: self.window!) } func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { print("\(response.notification.request.content.userInfo)") if response.actionIdentifier == "yes"{ print("True") }else{ print("False") } } func showFirebaseNotificationAlertFromAppDelegate(title: String, message: String, window: UIWindow){ let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) let action = UIAlertAction(title: "OK", style: .default, handler: nil) alert.addAction(action) window.rootViewController?.present(alert, animated: true, completion: nil) } } 文档。

要通过firebase发送推送消息,您需要拥有FCM令牌。您正在使用的令牌是来自APNS服务器的令牌,您需要将其转发到firebase。


0
投票

我得到了enter image description here

我应该在 func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { Messaging.messaging().apnsToken = deviceToken } 之前添加https://firebase.google.com/docs/cloud-messaging/ios/client

我还必须在Messaging Delegate中添加以接收FCM注册令牌。

answer from here里面添加Messaging.messaging().delegate = self

FirebaseApp.configure()

****同样在AppDelegate文件的底部添加Messaging Delegate及其方法。这是收到didFinishLaunching的地方:

Messaging.messaging().delegate = self
© www.soinside.com 2019 - 2024. All rights reserved.