Reputation: 2962
I just did an upgrade to all my Firebase dependencies and I have an issue, FirebaseInstanceId
is not recognized anymore. I have used it to get the token id like this:
String tokenId = FirebaseInstanceId.getInstance().getToken();
This is the error:
Cannot resolve symbol 'FirebaseInstanceId'
Here is my build.gradle file:
//Firebase
implementation 'com.google.firebase:firebase-auth:16.0.1'
implementation 'com.google.firebase:firebase-database:16.0.1'
implementation 'com.google.firebase:firebase-firestore:17.0.1'
//FirebaseUI
implementation 'com.firebaseui:firebase-ui-auth:4.0.0'
implementation 'com.firebaseui:firebase-ui-database:4.0.0'
implementation 'com.firebaseui:firebase-ui-firestore:4.0.0'
How can I get the token id using the latest dependencies?
Upvotes: 28
Views: 27284
Reputation: 1
fun setNewFcm() {
FirebaseMessaging.getInstance().token.addOnCompleteListener { task ->
if (!task.isSuccessful) {
return@addOnCompleteListener
}
if (task.result != null) {
val token: String = task.result
AppSharedData.setFcmToken(token)
}
}
}
Or by using this
fun setNewFcm() {
FirebaseInstallations.getInstance().getToken(true)
.addOnCompleteListener { task ->
if (!task.isSuccessful) {
return@addOnCompleteListener
}
if (task.result != null) {
val token: String = task.result.token
AppSharedData.setFcmToken(token)
}
}
}
Upvotes: 0
Reputation: 1
try this:
Add these dependencies to your gradle file:
implementation 'com.google.firebase:firebase-messaging:23.0.5'
implementation 'com.google.firebase:firebase-core:21.0.0'
Get firebase instance with FirabaseMessaging
String deviceToken = FirebaseMessaging.getInstance().getToken().toString();
Upvotes: 0
Reputation: 541
I also faced the same problem. From the doc, it says FirebaseInstanceId is no longer available and says to use FirebaseMessaging.getInstance().token as below
FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task ->
if (!task.isSuccessful) {
Log.w(TAG, "Fetching FCM registration token failed", task.exception)
return@OnCompleteListener
}
// Get new FCM registration token
val token = task.result
})
Upvotes: 37
Reputation: 69
I was also facing the same problem. Actually, FirebaseInstanceId has been shut down, replaced with Firebase Installation.
Try:
FirebaseInstallations.getToken();
Upvotes: 6
Reputation: 562
Add into Gradle:
implementation 'com.google.firebase:firebase-messaging:22.0.0'
implementation 'com.google.firebase:firebase-core:19.0.0'
Then add the following code to get the token:
public static String returnMeFCMtoken() {
final String[] token = {""};
FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull Task<String> task) {
if(task.isComplete()){
token[0] = task.getResult();
Log.e("AppConstants", "onComplete: new Token got: "+token[0] );
}
}
});
return token[0];
}
Upvotes: 11
Reputation: 80914
add the following to the build.gradle file:
implementation 'com.google.firebase:firebase-messaging:17.0.0'
implementation 'com.google.firebase:firebase-core:16.0.0'
more info here:
https://firebase.google.com/docs/cloud-messaging/android/client#set-up-firebase-and-the-fcm-sdk
Upvotes: 15