在日志的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);
}
}
最佳答案
另外,请确保在AndroidManifest.xml中正确定义了FirebaseInstanceIdService
在 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) {
}}