我正在尝试使用 Firebase Notification Composer 向我的设备发送测试通知。我正在接收 FCM token 并将其打印到我的控制台,然后我尝试向该 token 发送通知。
这是我检查过的内容:
1) 我在 iOS 12.4.1
2) 我正在分配消息传递委托(delegate)
3) 我从委托(delegate)方法接收 FCM token
4)我已经通过打印到控制台验证了通知已启用,当我被提示允许通知时,我单击了允许
5) 我已经验证在 Firebase 项目设置中有一个 APNs Auth Key 上传,具有正确的 TeamID 和 KeyID
6) 发送测试消息时没有调用委托(delegate)方法 willPresentNotifications
7)我尝试过启用/禁用 Swizzling,但都不起作用
8)调试没有错误
这是代码:
import UIKit
import Firebase
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UITabBar.appearance().barTintColor = UIColor(named: "Splish")!
UINavigationBar.appearance().barTintColor = UIColor(named: "Splish")!
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
FirebaseApp.configure()
Messaging.messaging().delegate = self
UNUserNotificationCenter.current()
.requestAuthorization(options: [.alert, .sound, .badge]) {granted, error in
print("Permission granted: \(granted)")
}
UNUserNotificationCenter.current().delegate = self
application.registerForRemoteNotifications()
return true
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("FCM Token Is: \(fcmToken)")
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("Token is: \(deviceToken)")
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Error is \(error)")
}
最佳答案
对于使用 Swift 的 iOS 14.4,我使用了 Firebase 的示例 here .
在@C6Silver 的帮助下建议在功能中启用“推送通知”,然后我能够接收 fcm 测试通知。
祝大家编码愉快!
关于ios - Firebase 云消息传递未收到通知 (iOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61032022/