MyFirebaseInstanceIdService没有调用为什么? [英] MyFirebaseInstanceIdService didn't invoked why?
问题描述
我已经启动MyFirebaseInstanseIdService
以获得token
,我将直接向您显示代码:
I have initiated MyFirebaseInstanseIdService
in order to get token
, directly i will show you the code :
@Override
public void onTokenRefresh() {
super.onTokenRefresh();
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
storeRegIdInPref(refreshedToken);
sendRegistrationToServer(refreshedToken);
Intent registrationComplete = new Intent(Config.REGISTRATION_COMPLETE);
registrationComplete.putExtra("token", refreshedToken);
LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}
private void sendRegistrationToServer(final String token) {
// sending gcm token to server
Log.e(TAG, "sendRegistrationToServer: " + token);
}
private void storeRegIdInPref(String token) {
SharedPreferences pref = getApplicationContext().getSharedPreferences(Config.SHARED_PREF, 0);
SharedPreferences.Editor editor = pref.edit();
editor.putString("regId", token);
editor.commit();
}
我的manifest.xml:
My manifest.xml :
<service android:name=".services.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".services.MyFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
我也尝试通过共享首选项在我的活动中通过broadCast获取令牌,但似乎未调用该类.
I tried to get token via broadCast in my activity also with shared prefrences, but it seems this class didn't invoked.
在我的log
中,我看到了以下几行:
In my log
i see those lines :
D/FirebaseApp: com.google.firebase.auth.FirebaseAuth is not linked. Skipping initialization.
D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
还有这个
I/FirebaseInitProvider: FirebaseApp initialization successful
推荐答案
The onTokenRefresh()
method only gets called when the current token expires. The current token could expire for the following reasons:
应用程序删除实例ID
App deletes Instance ID
应用已在新设备上还原
用户卸载/重新安装应用
User uninstalls/reinstall the app
用户清除应用数据
在某些情况下,第一次调用getToken()
时,它可能会返回一个null
值,您应该从该值开始触发onTokenRefresh()
.
There's also the scenario where the first time you call getToken()
, it might return a null
value, from where you should expect onTokenRefresh()
to trigger.
这样,您应该在初始活动中调用getToken()
来生成令牌.
With that, you should call getToken()
on your initial activity to generate a token.
这篇关于MyFirebaseInstanceIdService没有调用为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!