【问题标题】:swift cannot update FCM tokenswift 无法更新 FCM 令牌
【发布时间】:2020-04-11 01:37:40
【问题描述】:

如果有新令牌可用,我正在尝试更新我的 FCM 令牌。不幸的是,我收到了这个错误:

在隐式展开可选值时发现 nil

这是我的代码:

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)
    if Auth.auth().currentUser == nil{

    }else{
        if Auth.auth().currentUser!.uid == nil{

        }else{
            db.collection("Users").document(Auth.auth().currentUser!.uid).setData([ "fcm": fcmToken ], merge: true)

        }
    }

我认为是因为我的 uid 为 nil 但我之前正在检查它

【问题讨论】:

  • 哪一行........

标签: swift firebase google-cloud-platform firebase-cloud-messaging


【解决方案1】:

currentUser 是可选的,但currentUser 上的uid 不是。尝试像这样安全地打开 currentUser

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)

    if let currentUser = Auth.auth().currentUser {
        db.collection("Users").document(currentUser.uid).setData([ "fcm": fcmToken ], merge: true)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-24
    • 2017-05-10
    • 2020-09-07
    • 2017-11-09
    相关资源
    最近更新 更多