我正在将我的应用从 GCM 迁移到 FCM.
I am migrating my app from GCM to FCM.
当新用户安装我的应用程序时,会自动调用 onTokenRefresh()
.问题是用户还没有登录(没有用户ID).
When a new user installs my app, the onTokenRefresh()
is automatically being called. The problem is that the user is not logged in yet (No user id).
用户登录后如何触发onTokenRefresh()
?
onTokenRefresh()
方法将在生成新令牌时被调用.应用程序安装后,它将立即生成(正如您所发现的那样).当令牌发生变化时也会调用它.
The onTokenRefresh()
method is going to be called whenever a new token is generated. Upon app install, it will be generated immediately (as you have found to be the case). It will also be called when the token has changed.
根据 FirebaseCloudMessaging
指南:
您可以将通知定位到单个特定设备.在初始启动您的应用程序时,FCM SDK 会生成一个注册令牌客户端应用实例.
You can target notifications to a single, specific device. On initial startup of your app, the FCM SDK generates a registration token for the client app instance.
来源链接:https://firebase.google.com/docs/notifications/android/console-device#access_the_registration_token
这意味着令牌注册是针对每个应用程序的.听起来您想在用户登录后使用令牌.我建议您将 onTokenRefresh()
方法中的令牌保存到内部存储或共享首选项.然后,在用户登录后从存储中检索令牌,并根据需要向您的服务器注册令牌.
This means that the token registration is per app. It sounds like you would like to utilize the token after a user is logged in. What I would suggest is that you save the token in the onTokenRefresh()
method to internal storage or shared preferences. Then, retrieve the token from storage after a user logs in and register the token with your server as needed.
如果您想手动强制 onTokenRefresh()
,您可以创建一个 IntentService 并删除令牌实例.然后,当你调用getToken时,onTokenRefresh()
方法会再次被调用.
If you would like to manually force the onTokenRefresh()
, you can create an IntentService and delete the token instance. Then, when you call getToken, the onTokenRefresh()
method will be called again.
示例代码:
public class DeleteTokenService extends IntentService
{
public static final String TAG = DeleteTokenService.class.getSimpleName();
public DeleteTokenService()
{
super(TAG);
}
@Override
protected void onHandleIntent(Intent intent)
{
try
{
// Check for current token
String originalToken = getTokenFromPrefs();
Log.d(TAG, "Token before deletion: " + originalToken);
// Resets Instance ID and revokes all tokens.
FirebaseInstanceId.getInstance().deleteInstanceId();
// Clear current saved token
saveTokenToPrefs("");
// Check for success of empty token
String tokenCheck = getTokenFromPrefs();
Log.d(TAG, "Token deleted. Proof: " + tokenCheck);
// Now manually call onTokenRefresh()
Log.d(TAG, "Getting new token");
FirebaseInstanceId.getInstance().getToken();
}
catch (IOException e)
{
e.printStackTrace();
}
}
private void saveTokenToPrefs(String _token)
{
// Access Shared Preferences
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
// Save to SharedPreferences
editor.putString("registration_id", _token);
editor.apply();
}
private String getTokenFromPrefs()
{
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
return preferences.getString("registration_id", null);
}
}
编辑
FirebaseInstanceIdService
公共类 FirebaseInstanceIdService 扩展服务
public class FirebaseInstanceIdService extends Service
此类已弃用.赞成压倒一切FirebaseMessagingService 中的 onNewToken.一旦那已经实施后,可以安全地删除此服务.
This class is deprecated. In favour of overriding onNewToken in FirebaseMessagingService. Once that has been implemented, this service can be safely removed.
onTokenRefresh() 已弃用.在 MyFirebaseMessagingService
onTokenRefresh() is deprecated. Use onNewToken()
in MyFirebaseMessagingService
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onNewToken(String s) {
super.onNewToken(s);
Log.e("NEW_TOKEN",s);
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
}
}
这篇关于Firebase FCM 强制 onTokenRefresh() 被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!