本文介绍了尝试在空对象引用上调用虚拟方法com.google.firebase.iid.FirebaseInstanceId.getInstanceId()'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要使用firebase-messaging
库,并尝试在应用启动时使用以下方法获取令牌.
I am to using firebase-messaging
library and trying to fetch the token using below method on app launch.
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(new OnSuccessListener() {
@Override
public void onSuccess(InstanceIdResult instanceIdResult) {
String token = instanceIdResult.getToken();
// print token
}
});
应用在投放本身时崩溃
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.tasks.Task com.google.firebase.iid.FirebaseInstanceId.getInstanceId()' on a null object reference
AndroidManifest
AndroidManifest
<service
android:name=".MyFirebaseMessageService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
Build.gradle:
Build.gradle :
implementation 'com.google.android.gms:play-services-analytics:16.0.5'
implementation 'com.google.firebase:firebase-messaging:17.3.4'
此外,我尝试将令牌存储在SharedPreferences中,但似乎从未调用过onNewToken().
Also, i have tried to cache the token in SharedPreferences but it seems onNewToken() is never called.
@Override
public void onNewToken(String token) {
super.onNewToken(token);
SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preference.edit();
editor.putString("TOKEN",token);
editor.apply();
}
可能是什么问题?
推荐答案
合并清单中存在问题,合并清单中缺少以下服务.在AndroidManifest.xml
中添加了相同内容,就像是一种魅力.
There was problem in merged manifest, following service was missing from the merged manifest.Added same to AndroidManifest.xml
it worked like a charm.
<service android:name="com.google.firebase.components.ComponentDiscoveryService" >
<meta-data
android:name="com.google.firebase.components:com.google.firebase.iid.Registrar"
android:value="com.google.firebase.components.ComponentRegistrar" />
</service>
现在一切正常.
这篇关于尝试在空对象引用上调用虚拟方法com.google.firebase.iid.FirebaseInstanceId.getInstanceId()'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!