dav*_*as 16 push-notification access-token ios firebase firebase-cloud-messaging
我正在从 firebase 接收带有 APN 的通知的消息。在 Firebase 中,我有 APNs 密钥的证书,与从 Apple Developer 中提取的 Firebase 中的 Xcode 项目中具有相同的 id。
但我不知道为什么会发生这种情况,我收到此错误,它正在消息传递扩展中注册两个令牌:
extension AppDelegate : MessagingDelegate {
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {}}
Run Code Online (Sandbox Code Playgroud)
在检索发件人 ID“########”的 FCM 令牌之前未设置 APNS 设备令牌。对此 FCM 令牌的通知将不会通过 APNS 传递。设置 APNS 设备令牌后,请务必重新检索 FCM 令牌。
添加了我在 AppDelegate 中的内容
import Firebase
import MasivPushIosSdk
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate{
var firebaseToken: String = ""
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
self.registerForFirebaseNotification(application: application)
Messaging.messaging().delegate = self
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
}
func registerForFirebaseNotification(application: UIApplication) {
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()
}
}
extension AppDelegate: MessagingDelegate, UNUserNotificationCenterDelegate {
//MessagingDelegate
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
self.firebaseToken = fcmToken!
print("Firebase token: \(fcmToken)")
}
//UNUserNotificationCenterDelegate
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print("APNs received with: \(userInfo)")
}
}
Run Code Online (Sandbox Code Playgroud)
这是仅模拟器的日志。您可以安全地忽略它。您收到此消息的原因是 Firebase 尝试创建从 FCM 令牌到 APNS 令牌的映射,以便它可以将 APNS 消息发送到 iOS 设备。但是,模拟器上没有 APNS 令牌,因此映射失败。
尝试在实际设备上进行测试,看看是否仍然出现错误。
我在这个问题上花了很长时间。我终于让它与远程消息一起工作了。
它可能是由几个主要原因引起的。
Run Code Online (Sandbox Code Playgroud)const authStatus = await firebase.messaging().requestPermission(); if (authStatus === 1) { let fcmToken = await firebase.messaging().getToken(); if (fcmToken) { console.log('Firebase Push Token is:', fcmToken); } }
有关更多信息:A) 如果本地工作: https: //firebase.google.com/docs/cloud-messaging/ios/client B) 如果使用 React Native: https: //rnfirebase.io/messaging/usage/ios-设置
注意:不要混合使用 A 和 B。一个适用于原生 IOS,另一个适用于 React Native。
ps 我添加了 React Native ,因为我在使用它时在搜索中遇到了你的问题。
小智 5
这可能会迟到,但是错误
在检索发件人 ID“########”的 FCM 令牌之前未设置 APNS 设备令牌。对此 FCM 令牌的通知将不会通过 APNS 传递。设置 APNS 设备令牌后,请务必重新检索 FCM 令牌。
通过添加此内容为我排序
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("Yay! Got a device token \(deviceToken)")
Messaging.messaging().setAPNSToken(deviceToken, type: .unknown)
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
42668 次 |
最近记录: |