简体   繁体   English

“ didReceiveRegistrationToken”没有给我fcmToken吗?

[英]“didReceiveRegistrationToken” does not give me the fcmToken?

I'm building an app with firebase, and trying to save the FCM token for push notifications (which is sent by firebase functions). 我正在使用Firebase构建应用程序,并尝试保存FCM令牌以用于推送通知(由Firebase函数发送)。

The way I'm trying to do it is my storing the deviceToken in UserDefault, to use it later on. 我尝试执行此操作的方法是将deviceToken存储在UserDefault中,以便以后使用。

I need to save the fcmToken when the user signs up (to store it in my database under profile). 用户注册时,我需要保存fcmToken(以将其存储在配置文件下的我的数据库中)。

When the user signs out, I need to delete the fcmToken in the database. 当用户注销时,我需要删除数据库中的fcmToken。

Because of this I have my func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) in AppDelegate (maybe this is wrong?), and I store it like this: 因此,我在AppDelegate中存储了func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) (也许这是错误的?),我将其存储为:

AppDelegate code: AppDelegate代码:

func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {

        UserDefaults.standard.setValue("fcmToken", forKey: fcmToken)

}

When I want to call it, for example in the Sign Up viewcontroller, I declare the device token like this: 当我想调用它时,例如在“注册”视图控制器中,我这样声明设备令牌:

Sign Up ViewController code: 注册ViewController代码:

let deviceToken = UserDefaults.standard.value(forKey:"fcmToken") as? String ?? "not found"

And then i save it in my database like this 然后我像这样将其保存在我的数据库中

["fcmToken": deviceToken]

However, this always returns "not found" in Firebase, and does not save the actual device ID. 但是,这始终在Firebase中返回“未找到”,并且不保存实际的设备ID。 It would be great if you guys could help me out on this one. 如果你们能帮我解决这个问题,那就太好了。

You have your "" backwards when saving to UserDefaults. 保存到UserDefaults时,您的“”向后。 the way you have it you're saving the string "fcmToken" under the fcmToken String 您拥有的方式是将字符串“ fcmToken”保存在fcmToken字符串下

 func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {

    print(fcmToken)
    UserDefaults.standard.set(fcmToken, forKey: "fcmToken")

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 swiftUI 会为我提供正确代码的错误? - Why does swiftUI give me errors for correct code? 为什么 Int(Float(Int.max)) 给我一个错误? - Why does Int(Float(Int.max)) give me an error? 为什么 GeometryReader 似乎给了我错误的值? - Why does GeometryReader seem to give me wrong values? 为什么Swift会在这里给我EXC_BAD_INSTRUCTION? - Why does Swift give me an EXC_BAD_INSTRUCTION here? 符合Codable是否像符合NSCoding一样免费为我提供了Core Data价值转换? - Does conforming to Codable give me Core Data value transformation for free like conforming to NSCoding did? 如果主队列始终保持同步,为什么dispatchQueue.main会给我提供sync或async的选项 - Why does dispatchQueue.main give me the option of sync or async if main queue is always sync DidSelectRowAtIndextPath给我错误的详细信息 - DidSelectRowAtIndextPath give me wrong details FCM令牌不要调用didReceiveRegistrationToken - FCM token don't call didReceiveRegistrationToken 当我的便利初始化程序在if.-let语句中运行self.init时,为什么Swift会给我一个编译器错误? - Why does Swift give me a compiler error when my convenience initializer runs self.init inside if an if-let statement? 调用 didReceiveRegistrationToken 后如何检索 fcm 令牌? - How to retrieve fcm token after didReceiveRegistrationToken was called?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM