在我的 MainActivity
在我的日志中,我可以看到使用 FirebaseInstanceId.getInstance().getToken()
的 token 并显示生成的 token 。但似乎在我的 MyFirebaseInstanceIDService
延伸到 FirebaseInstanceIdService
的地方,onTokenRefresh()
没有被调用,在这个函数中它被说 token 最初是在此处生成的。我需要调用 sendRegistrationToServer()
这就是为什么我想知道它为什么不进入 onTokenRefresh()
的原因。
这是我的代码
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
sendRegistrationToServer(refreshedToken);
}
}
最佳答案
onTokenRefresh in FirebaseInstanceIdService is only called when a new token is generated. If your app was previously installed and generated a token then onTokenRefresh would not be called. Try uninstalling and reinstalling the app to force the generation of a new token, this would cause onTokenRefresh to be called.
还要确保您的 FirebaseInstanceIdService 在您的 AndroidManifest.xml 中正确定义
在您的 list 文件中。
<service
android:name="com.bnt.etailers.fcm.MyFireBaseInstanceIDService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service
android:name="com.bnt.etailers.fcm.GCMNotificationIntentService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
FirebaseInstanceIdService 类
public class MyFireBaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = MyFireBaseInstanceIDService.class.getSimpleName();
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
if (refreshedToken!=null) {
SettingPreferences.setStringValueInPref(this, SettingPreferences.REG_ID, refreshedToken);
}
// TODO: Implement this method to send any registration to your app's servers.
sendRegistrationToServer(refreshedToken);
}
// [END refresh_token]
/**
* Persist token to third-party servers.
*
* Modify this method to associate the user's FCM InstanceID token with any server-side account
* maintained by your application.
*
* @param token The new token.
*/
private void sendRegistrationToServer(String token) {
// Add custom implementation, as needed.
}}
FirebaseMessagingService 类。
public class GCMNotificationIntentService extends FirebaseMessagingService {
// Sets an ID for the notification, so it can be updated
public GCMNotificationIntentService() {
super();
}
@Override
public void onMessageReceived(RemoteMessage message) {
}}
关于android - Firebase onTokenRefresh() 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37451395/