在使用Firebase进行推送通知时,“onTokenRefresh”和“getToken”有什么区别?

3

我是一个firebase和react-native的新手,所以对于问题感到抱歉,但我想知道。

  1. what is the different between onTokenRefresh to getToken in push notification with firebase?

  2. why not use only with getToken?

  3. why onTokenRefresh is always occurs in this code, I thought it will run this function only if the token actually changed...

    componentDidMount() {
    
    kittensApp.onReady().then(app => {
     app.messaging().getToken()
    .then(fcmToken => {
    if (fcmToken) {
        console.log('fcmtokenApp', fcmToken)
    } else {
      console.log('here 2')
     } 
      })
    
    app.messaging().onTokenRefresh(token => {
    console.log('refreshtoken', token)
      })
         })}
    
  4. another question is in case I want to get the token and send it to the server to save it in the database for pushing notification in the future in what function I should use with (the get token or the refresh)? and how can I make sure that in case the token change (by updating the app or reinstall) it will send the new token to the server? or there is any way to keep the token new without make the user open the app?

感谢您的帮助!

1个回答

1
  1. 当新的令牌生成时,onTokenRefresh会使用上一个注册的令牌触发,但getToken用于注册令牌消息(如果您已经注册了令牌,则会从else语句中获取console.log)。
  2. 当新的令牌生成时,您不会获得正确的最新令牌。
  3. onTokenRefresh始终为您提供正确和最新的令牌。
  4. 在getToken(如果您尚未注册令牌并获得一个)和刷新(如果更改了令牌)时,您必须在服务器上比较它是否与数据库和应用程序中接收到的令牌相同。很好的选择是还将用户数据保存到例如redux存储器中,如果firebase刷新给您不同的令牌,请使用此新请求,否则不要做任何事情,因为它是正确的。

希望这能有所帮助。


2
谢谢你的回答...我还有一个小问题,假设用户已经登录并给了我设备令牌,我将其保存在服务器上的数据库中,现在用户重新安装了应用程序并且尚未打开应用程序,在这种情况下,设备令牌已更改,并且在数据库中我有无效的令牌,所以下次尝试推送通知时它将不起作用...有什么办法解决吗? - Tal Shani
@TalShani 我在React Native中使用OneSignal时也遇到了同样的问题,如果用户已登录并重新安装应用程序,则没有简单的方法可以注销他(删除令牌)并在JS中处理卸载/重新安装应用程序,在本机Android Activity或iOS中可能可以在服务中处理它,但我不确定是否可能(我只是跳过了这种情况)。用户必须在重新安装后运行应用程序,以便获得新令牌,并在服务器端更新它。如果他不打开应用程序,他将不会收到推送通知。 - Robson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接