为什么没有调用 MyFirebaseInstanceIdService?
MyFirebaseInstanceIdService didn't invoked why?
为了得到token
我发起了MyFirebaseInstanseIdService
,直接给大家看代码:
@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 :
<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>
我试图通过我的 activity 中的 broadCast 获取令牌,也使用共享首选项,但似乎这个 class 没有被调用。
在我的 log
中,我看到了这些行:
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
onTokenRefresh()
方法仅在当前令牌过期时被调用。当前令牌可能因以下原因而过期:
App deletes Instance ID
App is restored on a new device
User uninstalls/reinstall the app
User clears app data
还有一种情况是,您第一次调用 getToken()
时,它可能会 return 一个 null
值,您应该会从那里触发 onTokenRefresh()
。
因此,您应该在初始 activity 上调用 getToken()
以生成令牌。
为了得到token
我发起了MyFirebaseInstanseIdService
,直接给大家看代码:
@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 :
<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>
我试图通过我的 activity 中的 broadCast 获取令牌,也使用共享首选项,但似乎这个 class 没有被调用。
在我的 log
中,我看到了这些行:
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
onTokenRefresh()
方法仅在当前令牌过期时被调用。当前令牌可能因以下原因而过期:
App deletes Instance ID
App is restored on a new device
User uninstalls/reinstall the app
User clears app data
还有一种情况是,您第一次调用 getToken()
时,它可能会 return 一个 null
值,您应该会从那里触发 onTokenRefresh()
。
因此,您应该在初始 activity 上调用 getToken()
以生成令牌。