user2128702
user2128702

Reputation: 2121

Can not get expo push token on Android

I am building a React native app which is based on Expo and I want to also take advantage of Expo's push notification capabilities in a combination with Firebase realtime database. I am basically following the steps being described here. Everything was going pretty well before reaching the part where push notification token has to be extracted.

registerForPushNotificationsAsync = async (userInfo) => {
    // ... Stuff

    // Get the token that uniquely identifies this device
    let token = await Notifications.getExpoPushTokenAsync();
    console.log("Expo token: " + token);
    var updates = {}
    updates['/expoToken'] = token
    firebase.database().ref('Users').child(userInfo.user.uid).update(updates)
  }

The issue occurs on this line:

let token = await Notifications.getExpoPushTokenAsync();

It never returns a value and I can not obtain a push token. This is Android specific(Android 7.0) and it doesn't occur on an iPhone (iOS 11.0.2). On the iPhone a token is returned and I successfully store it in the database.

What I am doing is to start expo using their CLI and scan the QR code generated(using their client apps for IOS/Android). Then, again in the browser loaded CLI I observe what is happening runtime with the app respectively on the iPhone and on the Android phone. The versions of the packages are as follows:

[email protected]

[email protected]

[email protected]

I want to get this running on the Android but still with no success. I saw on the i-net that some people just use FCM for Push Notifications but still not sure. The way I understand it:

Note that FCM cannot be used to send messages to the Android Expo Client. Also, FCM is not currently available for Expo iOS apps.

is that using FCM you can never have the push notifications working for both IOS and Android.

EDIT: After an updated of the Android client app, now I am getting a message saying:

[Error: Couldn't get GCM token for device]

Upvotes: 3

Views: 5049

Answers (4)

44kksharma
44kksharma

Reputation: 2830

  1. sign up with expo if not done already.
  2. Install the expo-cli if you have not already done so.
  3. Log in to expo on your cli using "expo login".
  4. Run your app using "expo start".

Upvotes: 1

Carlos Navarro
Carlos Navarro

Reputation: 71

In addition to configuring the FCM, you must log in to your expo account (or create one), otherwise the same error will still appear.

Upvotes: 0

Zayco
Zayco

Reputation: 327

[Error: Couldn't get GCM token for device]

Will also trigger if you are not logged in to the expo-cli.

Your login status is indicated in the top left corner of Expo Dev Tools.

If you don't see LOGGED IN AS "Username", Run expo login in the cli and provide your username and password.

Upvotes: 5

Cliche818
Cliche818

Reputation: 51

I had this problem, need to setup Firebase. It was quite hidden in the docs (https://docs.expo.io/versions/latest/guides/push-notifications):

For Android, you'll also need to upload your Firebase Cloud Messaging server key to Expo so that Expo can send notifications to your app. This step is necessary unless you are not creating your own APK and using just the Expo Client app from Google Play. Follow the guide on Using FCM for Push Notifications to learn how to create a Firebase project, get your FCM server key,and upload the key to Expo.

Upvotes: 0

Related Questions