Firebase onNewToken无法刷新我的令牌 [英] Firebase onNewToken not refreshing my token

查看:299
本文介绍了Firebase onNewToken无法刷新我的令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,每次令牌到期时,我都需要刷新,但是在d后面运行的服务不会触发onNewToken()

So, I need to refresh my token each time it expires, but the service runing behind ddoes not trigger onNewToken()

清单

 <service
            android:name=".fcm.MyFirebaseMessagingService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

代码

class MyFirebaseMessagingService: FirebaseMessagingService() {

    override fun onNewToken(token: String) {
        super.onNewToken(token)
        val currentUser = FirebaseAuth.getInstance().currentUser?.uid
        if(currentUser != null){
            FirebaseFirestore.getInstance().collection("user").document(currentUser).update("deviceToken",token)
        }
    }
}

我每次在应用程序中通过onStart()时是否都需要显式调用getInstanceID()?还是应该像我一样用新服务更新我的deviceToken?

Does I need to explicitly call getInstanceID() each time I go throught onStart() in my app ? or should this service update my deviceToken with the new one like I did ?

我已经使用旧的登录帐户对其进行了测试,但是通知尚未到达,因此,新令牌尚未刷新(我需要注销并再次登录才能再次接收通知)

I have tested it with an old logged in account but notifications did not arrived, hence, the new token has not been refreshed (I needed to logout and login again to receive notifications once more)

推荐答案

onNewToken. What documentation says:

在为默认Firebase项目生成新令牌时调用.

Called when a new token for the default Firebase project is generated.

在安装应用后,首次生成令牌时会调用此方法,如果令牌发生更改,则会再次调用.

This is invoked after app install when a token is first generated, and again if the token changes.

这是什么意思,一旦生成了令牌,直到必须更改推送令牌后,才会调用此方法.

What does it mean is that once the token has been generated this method will not be called until push token has to change.

启动应用程序时,您必须获取FirebaseInstanceId的实例并从中提取令牌.它将确保您获得有效的令牌.

When your app starts you must get instance of FirebaseInstanceId and extract token form it. It will guarantee that you get a valid token.

您应该将代码保留在onNewToken方法中,并添加下面提到的代码来处理已经生成令牌的情况:

You should leave the code in your onNewToken method as it is and add the code mentioned below to handle cases when the token is already generated:

FirebaseInstanceId.getInstance().instanceId.addOnSuccessListener { instanceIdResult: InstanceIdResult ->
        val currentUser = FirebaseAuth.getInstance().currentUser?.uid
        if(currentUser != null){
            FirebaseFirestore.getInstance().collection("user").document(currentUser).update("deviceToken", instanceIdResult.token)
        }
    }

更新:何时使用它.

首先,您必须了解两件事:

Update: when to use it.

Firstly, you have to understand two things:

  • onNewToken在缺少令牌且必须生成(在应用程序安装后)或令牌已过期且必须更改时调用. /li>
  • FirebaseInstanceId.getInstance().instanceId.addOnSuccessListener(...)没有生成任何令牌!它只会返回您当前可用的有效且可以安全使用推送通知令牌.在您的情况下,多次调用是没有用的.
  • onNewToken is called when a token is absent and has to be generated (after application installation) OR when the token has expired and has to be changed.
  • FirebaseInstanceId.getInstance().instanceId.addOnSuccessListener(...) is NOT generating any tokens! It only returns you currently available, valid, safe to use push notification token. There is no use to call it multiple times in your case.

因此,现在您已有onNewToken并准备处理令牌更改.好的.保持原状.

So now you have onNewToken in place and ready to process token changes. Good. Leave as is.

现在,当令牌已经生成,有效且可以使用时,您需要处理的情况.在这种情况下,不会调用onNewToken!现在轮到您手动请求将此令牌退还给您了.

Now what you need to handle is a case when a token is already generated, valid and ready to use. onNewToken will not be called in that case! Now it's your turn to manually request this token to be returned to you.

示例:

class YourActivity: Activity {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        uploadFcmToken()
    }

    private fun uploadFcmToken() {
        FirebaseInstanceId.getInstance().instanceId.addOnSuccessListener { instanceIdResult: InstanceIdResult ->
            val currentUser = FirebaseAuth.getInstance().currentUser?.uid
            if(currentUser != null){
                FirebaseFirestore.getInstance()
                                     .collection("user")
                                     .document(currentUser)
                                     .update("deviceToken", instanceIdResult.token)
            }
        }
    }
}

这篇关于Firebase onNewToken无法刷新我的令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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