Swift FIRMessaging推送通知令牌重置 [英] Swift FIRMessaging push notification token reset
问题描述
每当用户导航到他们的个人资料时,我都会这样做吗:
let令牌= FIRInstanceID.instanceID()。令牌()
ref.child(用户)。child(id).child(pushToken).updateChildValues([tokenid令牌])
您现在看到我要求用户在注册,但不知何故,似乎它仍然生成一个令牌ID,即使我拒绝请求。所以我的问题是,我怎样才能保持这个令牌ID更新?我必须在AppDelegate中做到这一点?
谢谢。帮助非常感谢!
为了正确地将令牌存储在数据库中,需要处理两种情况:当应用程序启动时,
- 当令牌被刷新时,
-
ol> - when the app starts
- when the token gets refreshed
首先通过调用FirebaseInst FIRInstanceID.instanceID()。令牌()
来获取app / main视图中的当前令牌,例如 viewDidLoad
:
if let token = FIRInstanceID.instanceID {
ref.child(Users)。child(id).child(pushToken).updateChildValues([tokenid:token])
}
由于当前令牌可能尚未生成,因此您需要在此代码中处理 nil
。 然后 tokenRefreshNotification :
$ b $ p $
func tokenRefreshNotification(notification:NSNotification){
if let refreshedToken = FIRInstanceID.instanceID()。token(){
ref。 child(Users)。child(id).child(pushToken).updateChildValues([tokenid:refreshedToken])
}
}
$ c $另外看到这个答案,我给了一个类似的Android用例: 39960445 / update-firebase-instance-id-on-app-update / 39960697#39960697>更新应用更新上的Firebase实例ID
I am currently working with push notifications in my new project right now, and I am therefore experimenting with the generation of push tokens. My question is, when do these expire? I store each users push token in my Firebase database, but I know for a fact that these tokens expire/re-generates themselves. So my question is, what is the best way to hold the push token up to date?
Would I simply do this every time the user navigates to their profile?:
let token = FIRInstanceID.instanceID().token()
ref.child("Users").child(id).child(pushToken).updateChildValues(["tokenid":token])
You see, as of right now I am asking the user to allow push notifications right after signup, but somehow it seems that it still generates a token id even though I deny the request. So my question is, how can I keep this token id updated? Would I have to do this in AppDelegate?
Thanks. Help is much appreciated!
解决方案 To correctly store the token in your database, you'll need to handle two cases:
First get the current token on app/main view starts by calling FirebaseInstFIRInstanceID.instanceID().token()
in for example viewDidLoad
:
if let token = FIRInstanceID.instanceID().token() {
ref.child("Users").child(id).child(pushToken).updateChildValues(["tokenid":token])
}
Since the current token may not have been generated yet, you need to handle nil
in this code.
Then also monitor for changes to the token by implementing tokenRefreshNotification
:
func tokenRefreshNotification(notification: NSNotification) {
if let refreshedToken = FIRInstanceID.instanceID().token() {
ref.child("Users").child(id).child(pushToken).updateChildValues(["tokenid":refreshedToken])
}
}
Also see this answer I gave to a similar use-case for Android: Update Firebase Instance ID on app update
这篇关于Swift FIRMessaging推送通知令牌重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!