Ios 如何以及何时为firebase消息保存用户APN令牌?
我想知道如何将APNs令牌保存到firebase realtime DB,以便在从云函数发送通知时使用 我尝试在Ios 如何以及何时为firebase消息保存用户APN令牌?,ios,swift,firebase,firebase-realtime-database,firebase-cloud-messaging,Ios,Swift,Firebase,Firebase Realtime Database,Firebase Cloud Messaging,我想知道如何将APNs令牌保存到firebase realtime DB,以便在从云函数发送通知时使用 我尝试在appdelegate中执行此操作: // This function is added here only for debugging purposes, and can be removed if swizzling is enabled. // If swizzling is disabled then this function must be implemen
appdelegate
中执行此操作:
// This function is added here only for debugging purposes, and can be removed if swizzling is enabled.
// If swizzling is disabled then this function must be implemented so that the APNs token can be paired to
// the FCM registration token.
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("APNs token retrieved: \(deviceToken)")
let ref = Database.database().reference()
ref.child("APNRegistrationToken").child((Auth.auth().currentUser?.uid)!).child("token").setValue(deviceToken)//Save the users token for use when reciving notification
// With swizzling disabled you must set the APNs token here.
// Messaging.messaging().apnsToken = deviceToken
}
}
这会导致错误(在某些情况下,用户尚未注册,因此没有currentUser.uid(通常我认为即使用户已注册,我也无法在应用程序代理中访问该uid))
如何以及在哪里保存APNs令牌?我很惊讶没有简单的答案,但下面是您的操作方法: 您只需在
appdelegate
中的一个位置保存:
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)
// TODO: If necessary send token to application server.
saveTokenToFire(deviceToken: fcmToken)//Save to a chosen location in DB
// Note: This callback is fired at each app startup and whenever a new token is generated.
}