Firebase FCM令牌-何时发送到服务器? [英] Firebase FCM token - When to send to server?

查看:130
本文介绍了Firebase FCM令牌-何时发送到服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我有一个应用程序,它首先启动时会带您浏览几张欢迎的幻灯片,然后将您带到登录/注册页面,然后再至MainActivity.

Okay so I have an app which on first start takes you through a few welcoming slides, then takes you to a login/register page and then to MainActivity.

我刚刚实现了FCM,并且服务在用户看到任何这些页面之前生成了token .如何才能使服务在我进入MainActivity之后运行?

I have just implemented FCM and the services generate a token before any of those pages have been seen by the user. How could I make it so that the service runs after I get to MainActivity?

问题是我试图将令牌刷新到MySQL DB后立即将其发送到适当的用户帐户,但是由于用户尚未登录,因此该消息就是null服务器失败.有什么好的设计方法?我曾考虑过将令牌保存在SharedPreferences中,并在用户登录后将其发送到服务器,但是当稍后刷新令牌时会带来很多麻烦?!

The problem is I'm trying to send the token as soon as it is refreshed to the MySQL DB to the appropriate user account, but since the user hasn't signed in yet, that is null and my message to the server fails. What's a good way to design this? I thought of saving the token in SharedPreferences and sending it to the server after the user has logged in but that creates lots of complications when the token is refreshed at some later point?!

可能的解决方案:

我不确定我是否完全理解这两种服务的运行方式,但是说在onTokenRefresh中,我只是将令牌保存到SharedPreferences中,而在MainActivity中,我从SP中获取了值,然后将其发送到服务器.在这种情况下,刷新令牌后,新值将立即再次进入SharedPreferences.但是我仍然需要检查它是否是SP中的新值,然后将其重新上传到服务器.这很令人困惑!

I'm not sure I completely understand how the 2 services run but say in onTokenRefresh I just save the token into SharedPreferences and in MainActivity I get the value from SP and then I send it to the server. In that case when the token is refreshed the new value will immediately go into SharedPreferences again. But I would still need to check if it's a new value in SP and then reupload it to the server. This is confusing!

推荐答案

是的,FCM令牌是自动生成的.但是,请尝试从另一个角度看待这个问题.

Yes FCM token is generated automatically. But try to see this in a different angle.

这就是我的处理方式.

让FCM在您的应用启动后立即生成令牌. OnTokenRefresh将被调用,您只需将其保存为以下首选项即可:

Let FCM generate token as soon as your app starts. OnTokenRefresh will be called and you just save it in your preferences as:

@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);

    sendRegistrationToServer(refreshedToken);
}

private void sendRegistrationToServer(String token) {
    // Add custom implementation, as needed.
    SharedPreferenceUtils.getInstance(this).setValue(getString(R.string.firebase_cloud_messaging_token), token);

   // To implement: Only if user is registered, i.e. UserId is available in preference, update token on server.
   int userId = SharedPreferenceUtils.getInstance(this).getIntValue(getString(R.string.user_id), 0);
   if(userId != 0){
       // Implement code to update registration token to server
   }
}

希望您清楚自己的做法.询问是否需要更多许可.

Hope you are clear with the way. Ask if you need more clearance on it.

这篇关于Firebase FCM令牌-何时发送到服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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