Swift FIRMessaging推送通知令牌重置 [英] Swift FIRMessaging push notification token reset

查看:250
本文介绍了Swift FIRMessaging推送通知令牌重置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在使用推送通知在我的新项目中工作,因此我正在试验推令牌的生成。我的问题是什么时候这些到期?我将每个用户推送令牌存储在我的Firebase数据库中,但是我知道这些令牌过期/重新生成自己。所以我的问题是,持续推送令牌是最新的最好方法是什么?



每当用户导航到他们的个人资料时,我都会这样做吗:

  let令牌= FIRInstanceID.instanceID()。令牌()

ref.child(用户)。child(id).child(pushToken).updateChildValues([tokenid令牌])

您现在看到我要求用户在注册,但不知何故,似乎它仍然生成一个令牌ID,即使我拒绝请求。所以我的问题是,我怎样才能保持这个令牌ID更新?我必须在AppDelegate中做到这一点?



谢谢。帮助非常感谢!

解决方案

为了正确地将令牌存储在数据库中,需要处理两种情况:当应用程序启动时,


  1. 当令牌被刷新时,

  2. ol>

    首先通过调用FirebaseInst FIRInstanceID.instanceID()。令牌()来获取app / main视图中的当前令牌,例如 viewDidLoad

      if let token = FIRInstanceID.instanceID {
    ref.child(Users)。child(id).child(pushToken).updateChildValues([tokenid:token])
    }

    由于当前令牌可能尚未生成,因此您需要在此代码中处理 nil 然后 tokenRefreshNotification :

    $ b监视令牌更改的监视器 - 监视器 - 令牌生成rel =nofollow noreferrer
    $ b $ p $ func tokenRefreshNotification(notification:NSNotification){
    if let refreshedToken = FIRInstanceID.instanceID()。token(){
    ref。 child(Users)。child(id).child(pushToken).updateChildValues([tokenid:refreshedToken])
    }

    }
    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:

    1. when the app starts
    2. when the token gets refreshed

    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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆