Reputation: 803
I am integrating Firebase Cloud messaging in android
app build.gradle looks like this
implementation 'com.google.firebase:firebase-auth:16.0.3'
implementation 'com.google.firebase:firebase-messaging:17.3.3'
implementation 'com.google.android.gms:play-services-auth:16.0.0'
apply plugin: 'com.google.gms.google-services'
class path in apllication level build.gradle is
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0'
classpath 'com.google.gms:google-services:4.0.1'
}
and my manifest is something like that
<service android:name=".firebase.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_artize_a" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id" />
My problem is that When I try to get FirebaseInstanceId.getInstance()
it returns null. so I am unable to get Token.
Even onNewToken
of MyFirebaseMessagingService
is not fired a single time in app.
Can Some one tell me why i am getting FirebaseInstanceId.getInstance() null
here thanks in advance.
Here i am trying to get firebase token
private void getFirebaseToken(){
FirebaseInstanceId firebaseInstanceId = FirebaseInstanceId.getInstance();
if(firebaseInstanceId == null){
return;
}
firebaseInstanceId.getInstanceId().addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
return;
}
// Get new Instance ID token
String token = task.getResult().getToken();
}
});
}
Upvotes: 6
Views: 5520
Reputation: 1540
After Long research, I found that tools:node="replace"
in the manifest file is making FCM to not work.
Change tools:node="replace"
to tools:node="merge"
go through this link
https://github.com/firebase/quickstart-android/issues/477
Upvotes: 7
Reputation: 91
try to replace in your Android Manifest application tag with the below attributes
android:allowBackup="false" tools:node="merge" tools:replace="android:label,android:allowBackup"
Upvotes: 3
Reputation: 21497
For me there was a missing declaration in the AndroidManifest.xml
<application ... >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
After adding it, the issue was resolved.
Upvotes: 0
Reputation: 1
Having FirebaseInstanceId.getInstance();
on the UI thread shouldn't be a problem. I have the following in the onCreate method of the MainActivity and it works perfectly (probably not recommended by google and others):
FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(instance_id_completion_listener);
Try the following steps:
Add implementation 'com.google.firebase:firebase-core:16.0.1'
to the app build.gradle file.
Remove, but back it up, all manifest entries related to Firebase. I don't have any in my manifest file however I am only using Firebase to get an instance id.
If one hasn't done so already go to https://firebase.google.com/docs/android/setup
and add Firebase to Your Android Project.
One could also try the following:
private void getFirebaseToken(){
FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
return;
}
Log.d("getFirebaseToken", "Successful");
// Get new Instance ID token
String token = task.getResult().getToken();
}
});
}
For testing purposes only as this is not a good idea on the UI thread, try the following:
private void getFirebaseToken(){
FirebaseInstanceId firebaseInstanceId = FirebaseInstanceId.getInstance();
try
{
Thread.sleep(2000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
if(firebaseInstanceId == null){
return;
}
firebaseInstanceId.getInstanceId().addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
return;
}
// Get new Instance ID token
String token = task.getResult().getToken();
}
});
}
Upvotes: 0