Can't get expoToken in Expo Notification, due to Failed resolution of: Lcom/google/firebase/iid/FirebaseInstanceId;

Good afternoon, hope you guys have a good day, i’m already building some apps using expo bare workflow, and need to use Expo Notification, i’m following all the instruction, from expo notification documentations, and fcm configurations, to setup firebase, but, when i’m tryin to get expoToken, i got this warning, looks like this:

Error: Encountered an exception while calling native method: Exception occurred while executing exported method getDevicePushTokenAsync on module ExpoPushTokenManager: Failed resolution of: Lcom/google/firebase/iid/FirebaseInstanceId;

and I’ve made sure that my package name is the same as the package I registered in firebase, can you guys help me? pleasee :slight_smile:

this is addtional information:

Getting Expo Token

const getToken = async () => {
    console.log("TOKEN CHECK");
    const { status: existingStatus } =
      await Notifications.getPermissionsAsync();
    let finalStatus = existingStatus;
    if (existingStatus !== "granted") {
      const { status } = await Notifications.requestPermissionsAsync();
      finalStatus = status;
    }
    if (finalStatus !== "granted") {
      alert("Failed to get push token for push notification!");
      return;
    }
    token = (await Notifications.getExpoPushTokenAsync()).data;
    console.log(token);
  };

App.js

{
  "name": "myapp",
  "displayName": "myapp",
  "expo": {
    "name": "myapp",
    "slug": "myapp",
    "version": "1.0.0",
    "assetBundlePatterns": [
      "**/*"
    ]
  },
  "android": {
    "googleServicesFile": "./google-services.json",
    "useNextNotificationsApi": true
  }
}

Package.json

{
  "main": "index.js",
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "web": "expo start --web",
    "start": "react-native start"
  },
  "dependencies": {
    "@dudigital/react-native-zoomable-view": "^1.0.16",
    "@react-native-async-storage/async-storage": "^1.15.5",
    "@react-native-community/masked-view": "^0.1.11",
    "@react-native-community/netinfo": "^6.0.0",
    "@react-native-firebase/app": "^12.0.0",
    "@react-native-firebase/firestore": "^12.0.0",
    "@react-native-firebase/storage": "^12.0.0",
    "@react-native-picker/picker": "^1.16.1",
    "@react-navigation/material-bottom-tabs": "^5.3.15",
    "@react-navigation/native": "^5.9.4",
    "@react-navigation/stack": "^5.14.5",
    "expo": "~41.0.1",
    "expo-av": "~9.1.2",
    "expo-image-manipulator": "~9.1.0",
    "expo-image-picker": "~10.1.4",
    "expo-notifications": "~0.11.6",
    "expo-splash-screen": "~0.10.2",
    "expo-status-bar": "~1.0.4",
    "expo-updates": "~0.5.4",
    "expo-web-browser": "~9.1.0",
    "firebase": "^8.6.5",
    "moment": "^2.29.1",
    "react": "16.13.1",
    "react-dom": "16.13.1",
    "react-native": "~0.63.4",
    "react-native-calendars": "^1.1263.0",
    "react-native-custom-qr-codes-expo": "^2.2.0",
    "react-native-datepicker": "^1.7.2",
    "react-native-gesture-handler": "~1.10.2",
    "react-native-linear-gradient": "^2.5.6",
    "react-native-orientation-locker": "^1.3.1",
    "react-native-paper": "^4.9.1",
    "react-native-raw-bottom-sheet": "^2.2.0",
    "react-native-reanimated": "~2.1.0",
    "react-native-render-html": "^5.1.1",
    "react-native-responsive-screen": "^1.4.2",
    "react-native-safe-area-context": "^3.2.0",
    "react-native-screens": "~3.0.0",
    "react-native-shimmer-placeholder": "^2.0.7",
    "react-native-svg": "12.1.0",
    "react-native-unimodules": "~0.13.3",
    "react-native-vector-icons": "^8.1.0",
    "react-native-web": "~0.13.12",
    "react-native-webview": "^11.6.2"
  },
  "devDependencies": {
    "@babel/core": "^7.9.0"
  },
  "private": true
}

screenshot:
image

Hey @sinarindoutama, on the Bare workflow you need to pass the experienceId in the options argument to getExpoPushTokenAsync. See the docs here.

Cheers,
Adam

Hey @adamjnav thanks for you reply, got it. i think i should look closer to this example.

I face the same problem in some days before.
And I don’t add experienceid.
But when you run getToken use it like getToken().then(res=>alert(res)).catch(err=>alert(err))
And U CAN’T get token in simulator/emulator and your console.log(token) wouldnot be work

Sorry, engl not my lang

I had the same issue. Notifications suddenly stop work.

I added dependencies and it solves the issue on my end

// android/app/build.gradle
dependencies {
   ...
   implementation 'com.google.firebase:firebase-iid:17.0.2'
   ...
}
4 Likes

@adamjnav Would the firebase-iid gradle dependency need to be added to the @react-native-firebase/app config plugin for EAS builds that was recently introduced in https://github.com/invertase/react-native-firebase/pull/5480?

If the dependency isn’t required in RNFB official installation steps, it won’t be included in the plugin.

If you need this dependency for your app to work, you can write your own plugin (there’s nothing wrong in storing it in your project dir) which adds that dependency. The easiest way to do it would be probably modifying the with-rn-firebase plugin code by changing it to com.google.firebase:firebase-iid:17.0.2 and removing other optionals.

The final plugin (I have not tested this code) may look like this:

//firebase-iid-plugin.js
const expoConfigPlugins = require("@expo/config-plugins");

const firebaseIID = "com.google.firebase:firebase-iid";
const iidVersion = "17.0.2";

function addDependency(buildGradle) {
  if (!buildGradle.includes(firebaseIID)) {
    // TODO: Find a more stable solution for this
    buildGradle = buildGradle.replace(
      /dependencies\s?{/,
      `dependencies {
        implementation '${firebaseIID}:${iidVersion}'`
    );
  }
  return buildGradle;
}

/**
 * Update `app/build.gradle` by adding firebase-iid dependency
 */
module.exports = function withIidDependency(config) {
  return expoConfigPlugins.withAppBuildGradle(config, (config) => {
    config.modResults.contents = addDependency(config.modResults.contents);
    return config;
  });
};

Then add it to your app.json:

"plugins": [..., "./firebase-iid-plugin"]
2 Likes

Great this seems to work: thanks for your help!