21

I am trying to use FirebaseInstanceId but keep getting the error

"Cannot resolve symbol FirebaseInstanceId".

The modules gradle includes

dependencies {
    compile 'com.google.firebase:firebase-messaging:+'
    compile 'com.google.firebase:firebase-iid:+'
}

The only thing which exists on com.google.firebase.iid seems to be .zzb. Am I missing something?

5 Answers 5

12

Make sure you have all of these

 implementation 'com.google.firebase:firebase-core:17.2.1'

 implementation 'com.google.firebase:firebase-messaging:20.0.0'
 implementation 'com.google.firebase:firebase-auth:19.1.0' // not necessary(required for signout and sign in)

Just this much is required.

3

Old questions but still relevant so here's an updated answer: As of now (sept 2020) only implementation 'com.google.firebase:firebase-messaging:20.2.4' is required in your app/build.gradle file (see referenced official doc).

To further add information that I've struggled to find elsewhere when I've researched how to implement push notifications for Android:
I'm assuming you're using FirebaseInstanceId to retrieve the Instance ID token created by Firebase and are following the guide (see linked documentation). If your main goal is to implement push notifications and you're using React Native I've found you don't need to create the MyFirebaseMessagingService that extends FirebaseMessagingService - you can implement the library react-native-firebase/app and react-native-firebase/messaging to access the token in clients App.

Install both @react-native-firebase/app and @react-native-firebase/messaging Then in your frontend App.js: import messaging from '@react-native-firebase/messaging';

async function requestUserPermission() {  
  const getFcmToken = async () => {
    const fcmToken = await messaging().getToken();
    if (fcmToken) {
      console.log(fcmToken);
      console.log('Your Firebase Token is:', fcmToken);
    } else {
      console.log('Failed', 'No token received');
    }
  };
  const authStatus = await messaging().requestPermission();
  const enabled =
    authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
    authStatus === messaging.AuthorizationStatus.PROVISIONAL;

  if (enabled) {
    getFcmToken();
    console.log('Authorization status:', authStatus);
  }
}
requestUserPermission();

I'm sure this can be refactored, please suggest edits :)
Official documentation Firebase
React Native Firebase library

1
  • This is JavaScript solution (ReactNative). Thanks for the link to docs. Jan 18, 2022 at 5:07
1

Only use the dependency firebase-messaging with firebase-core

firebase-iid is not required to be declared as dependency.

Here is the documentation : https://firebase.google.com/docs/cloud-messaging/android/client#set-up-firebase-and-the-fcm-sdk

1

This is the old version. FirebaseInstanceIdService is deprecated

package com.example.xxxxxxx.fcm;

public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService{

    @Override
    public void onTokenRefresh() {
        String refreshToken = FirebaseInstanceId.getInstance().getToken();
        sendRegistrationToServer(refreshToken);
    }

    private void sendRegistrationToServer(String refreshToken) {
    }
}

The new version is

package com.example.xxxxxxx.fcm;

import androidx.annotation.NonNull;

import com.google.firebase.messaging.FirebaseMessagingService;

public class MyFirebaseInstanceIdService extends FirebaseMessagingService {

    @Override
    public void onNewToken(@NonNull String token) {
        super.onNewToken(token);
        String refreshToken = FirebaseInstanceId.getInstance().getToken();
        sendRegistrationToServer(refreshToken);
    }

    private void sendRegistrationToServer(String refreshToken) {
    }
}

Here is the documentation: https://firebase.google.com/docs/cloud-messaging/android/client#set-up-firebase-and-the-fcm-sdk

1
  • In new version we cannot access refreshToken using FirebaseInstanceId.getInstance().getToken() right? Jun 29, 2022 at 9:29
0
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)
            }
        }
}
1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jun 29, 2022 at 6:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.