Expo Push Token doesn't generated on Android

Hello guys,

I face to a new problem today. All my notifications services are working fine on iOS avec the ExponentPushToken. But on android nothing is working and I don’t understand why. I follow this link from documentation of expo : Push Notifications Setup - Expo Documentation
I use the code of the documentation.

My code :

async function registerForPushNotificationsAsync() {
    var token;
    if (Constants.isDevice) {
		alert('is device')
      const { status: existingStatus } = await Notifications.getPermissionsAsync();
      let finalStatus = existingStatus;
      if (existingStatus !== 'granted') {
		alert('not granted')
        const { status } = await Notifications.requestPermissionsAsync();
        finalStatus = status;
      }
      if (finalStatus !== 'granted') {
		alert('Failed to get push token for push notification!');
        console.log("Can't get notifications from this device")
        return;
      }
	  alert('ici la mtn')
	  console.log(finalStatus, ' finalStatus ', existingStatus, ' existingStatus ');
      token = (await Notifications.getExpoPushTokenAsync()).data;
      console.log(token);
	  alert('token : ' + token)
    } else {
		alert('cant receive notifs')
      console.log("Not on a physical device. Can't receive notifications.")
    }
	alert('je suis ici')
    if (Platform.OS === 'android') {
	 alert('on android')
      Notifications.setNotificationChannelAsync('default', {
        name: 'default',
        importance: Notifications.AndroidImportance.MAX,
        vibrationPattern: [0, 250, 250, 250],
        lightColor: '#FF231F7C',
      });
    }
	alert('token: ' + token);
    return token;
}

I re-created many onces the firebase project I verified the api key the same in google-services.json and on the google cloud api platform. My App.json has been configured with the google-services.json
I follow line by line the documentation, and the evidence, on iOS it works… So why android is not working ? The application can’t create an exponent push token…

Note that on Expo Go, the application is capable to generate an exponent push Token, and it works. Expo go receive notifications.

Thanks for your help !

Hi, so since the same code is working on iOS and in Expo Go, it’s likely that something is misconfigured, so I would double and triple check that you’ve followed every single step here . Couple other things for you to try:

  • Have you logged the response you are getting from getExpoPushTokenAsync?
  • Have you checked adb logcat for any errors when you request the token?

Hey,

I don’t know really why but, now I can generate a token in production ! But when I try to send a notification the API answer that :

{"data":[{"id":"30a11888-c10f-48a9-aeae-ef5c8a0251b5","status":"error","message":"Failed to authenticate with the FCM

server. Ensure the FCM server key you uploaded is

correct.","details":{"error":"InvalidCredentials","fault":"developer","fcm":{"httpStatus":401,"response":"<HTML>\n

<HEAD>\n<TITLE>PROJECT_NOT_PERMITTED</TITLE>\n</HEAD>\n

<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\">\n<H1>PROJECT_NOT_PERMITTED</H1>\n<H2>Error 401</H2>\n</BODY>\n

</HTML>

\n"}}}

But my FCM Server key is good on my expo credentials I don’t understand

you can check the FCM server key for your project by running expo push:android:show from the project directory

Yeah the result of this command correspond to one of this keys. I created one other to check if its works, does I need to rebuild the application ?

You only need to rebuild if you’ve changed googleservices.json

have you confirmed that your API key in google-services.json has the correct “API restrictions” in the Google Cloud Platform API Credentials console? For push notifications to work correctly, Firebase requires the API key to either be unrestricted (the key can call any API), or have access to both Firebase Cloud Messaging API and Firebase Installations API . The API key can be found under the client.api_key.current_key field in google-services.json .

Yeah normally that’s good, sorry it’s in French but it’s not restricted

Okkay I found I big issue really interesting…
When Firebase generated the google-services.json the api key inside api_key.current_key was bad, that’s not the same than the google cloud platform ! So I replaced it with the good from google cloud platform ! But when I set to the old key (the bad generated by firebase) the application is capable to generate an exponent push token but with the goo api key she isn’t capable. That’s really interesting…
Does I have to re generate an API Key from google cloud platform ?

I’m not really sure how Firebase is giving you a bad API key, you may want to investigate more there…but it doesn’t seem like this is an Expo issue

Okkay i investigate an other issue, maybe it’s because fcm registration and cloud messaging api doesn’t activated in google cloud platform !
I keep you informed

So I activated all api to be sure to work with firebase, I reset the api.current_key to the firebase generated key.
So I don’t understand I’m disappointed… I can get the Exponent Token but the API answers the project not permitted

There is an issues here too… The firebase installation API got 13 request and 100% of errors…

this is my app.json

{
  "expo": {
    "name": "Spirit",
    "slug": "spirit-app",
    "version": "1.0.7",
    "orientation": "portrait",
    "icon": "./src/assets/logos/LOGO_APP.png",
    "splash": {
      "image": "./assets/splash.png",
      "resizeMode": "contain",
      "backgroundColor": "#ffffff"
    },
    "updates": {
      "fallbackToCacheTimeout": 0
    },
    "assetBundlePatterns": [
      "**/*"
    ],
    "ios": {
      "supportsTablet": false,
      "bundleIdentifier": "com.ml-dev.spirit.app",
      "buildNumber": "17",
      "infoPlist": {
        "NSCameraUsageDescription": "Spirit à besoin de votre caméra pour scanner votre carte bleue lors des paiements"
      }
    },
    "android": {
      "adaptiveIcon": {
        "foregroundImage": "./assets/adaptive-icon.png",
        "backgroundColor": "#FFFFFF"
      },
      "versionCode": 17,
      "package": "com.ml.dev.spirit.app",
      "googleServicesFile": "./google-services.json"
    },
    "web": {
      "favicon": "./assets/favicon.png"
    },
    "plugins": [
      [
        "@stripe/stripe-react-native",
        {
          "merchantIdentifier": "merchant.spirit.drink",
          "enableGooglePay": true
        }
      ]
    ]
  }
}

Yeah, there at least was an issue- these errors could’ve been from your earlier attempts with the bad key

@loucaplou I suggest removing your post that includes your FCM server keys in it

Okay so what’s the fix ? I have to remove my previous key in expo credentials ? Or reset cache somewhere ?

I still have issues, the api still answers with project not permitted, i don’t understand

You should start from a fresh project, with a fresh firebase project, and go from there so you have a clean slate. The fix is to configure the proper FCM API key in accordance with your google-services file

Alright, it’s done, firebase auto-generated my keys (it’s not the case before) so that’s cool but I have the same error, when I try to send notification from FCM directly it works fine, the application receive the notification, but when I try to send it from the expo notification tools or directly from the expo API it doesn’t work. The response is :

{"data":[{"id":"abaf1bba-0875-44a9-a90e-ff0501fd3188","status":"error","message":"Failed to authenticate with the FCM

server. Ensure the FCM server key you uploaded is

correct.","details":{"error":"InvalidCredentials","fault":"developer","fcm":{"httpStatus":401,"response":"<HTML>\n

<HEAD>\n<TITLE>PROJECT_NOT_PERMITTED</TITLE>\n</HEAD>\n

<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\">\n<H1>PROJECT_NOT_PERMITTED</H1>\n<H2>Error 401</H2>\n</BODY>\n

</HTML>

\n"}}}

So I don’t understand, I follow the docs line by line, I started from clean project with clean google-services.json with clean firebase and GCP project but it still doesn’t work…