好的,所以我有一个应用程序,它在第一次启动时会带您浏览几张欢迎幻灯片,然后带您进入登录/注册页面,然后进入 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?!
可能的解决方案:
我不确定我是否完全理解这 2 个服务的运行方式,但在 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 token 是自动生成的.但请尝试从不同的角度来看待这一点.
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 SDK (21.0.0) ,您需要重写 onNewToken()
方法而不是 onTokenRefresh()
Using the new Firebase SDK (21.0.0) , you need to override onNewToken()
method instead of onTokenRefresh()
@Override
public void onNewToken(@NonNull String s) {
super.onNewToken(s);
sendRegistrationToServer(s);
}
这篇关于Firebase FCM 令牌 - 何时发送到服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!