[英]“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.