Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何以及何时为firebase消息保存用户APN令牌?_Ios_Swift_Firebase_Firebase Realtime Database_Firebase Cloud Messaging - Fatal编程技术网

Ios 如何以及何时为firebase消息保存用户APN令牌?

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

我想知道如何将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 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.
}