Trying to setup FCM on an ExpoKit project... so close! (I think)

#1

Hi!

I’m trying to follow these directions to setup FCM on an ExpoKit project. Firebase analytics is definitely recognizing that my client is connecting. I’ve tried sending a notification using Firebase’s tester to all users of my app (aka just me). Firebase says the message is delivered, but I haven’t seen anything in the notification tray on the device.

My next step in troubleshooting was to try to send a notification to my specific device ID (as most FCM tutorials suggest), but, I’m not sure how to get it via Expo’s FCM setup. Even though the Expo Notifications API is generally geared towards non-detached users, I was thinking that Expo.Notifications.getDevicePushTokenAsync() may still work detached because Expo’s push server probably isn’t involved in obtaining a token from the device. However, the current API documentation only mentions obtaining a GCM token. Is it possible to obtain the FCM token from Expo’s API?

My other angle for troubleshooting was if notification channels could be affecting this. (I’m using an Android 7 device so I’m not sure if this configuration even matters). Either way, I’ll be supporting Android 8, so wondering if the Expo Notification API’s for creating notification channels should still work when detached (again, seems like a negotiation with the device, doesn’t seem like Expo’s servers should be involved until the notification is sent).

For what it’s worth, @jesse’s suggestion seems to jive with other tutorials for adding FCM (see this one, and Firebase’s own quickstart). The major difference being that the tutorials have you creating your own FCM service classes instead of using ExpoFcmMessagingService and ExpoFcmInstanceIDService, and that they don’t cover getting the device token over to JavaScript. It seems like leaning on Expo’s existing service classes is the much better path, since there already is some JS integration. Additionally, the vanilla React Native path (using react-native-firebase) asks us to do things that I believe would break other parts of an ExpoKit project, like upgrading to Gradle 4.4.

Thank you!

#2

Maybe answered a few of my questions!

Expo.Notifications.getDevicePushTokenAsync() does work! If I don’t specify any parameters, I get a object back with a data field containing the FCM token and a type field with “fcm” as the value. When I put that token into the Firebase message tester, I get a message in the tray- but only if the app is backgrounded (I’m not used to this from previous GCM usage- if someone can validate if this expected, that would be great).

So, actually, I get TWO notifications in my tray - one with the Expo icon and one with a blank circle. I haven’t setup a notification icon in my app yet, so the blank circle is not unexpected, but not sure why I’m getting two notifications with the same message. I’ve tried uninstalling Expo and builds I’ve done under different app ID’s for various reasons, but I still get two. I would think the tokens would be unique across apps, and I haven’t used that google-services.json elsewhere, so quite puzzling!

#3

Figured out the double notification thing. The GCM configuration in AndroidManifest.xml needs to be removed.

So, to expand on @jesse’s directions from the original post, here’s the steps as best I can tell so far to add FCM to an ExpoKit project:

  1. Copy over the google-services.json file in the android project folder with the one you get from Firebase when you create a new Android app on their console.
  2. Copy the FCM service declarations into AndroidManifest.xml referenced in this forum post. (from comments on later versions of this code, it sounds like maybe this is already in AndroidManifest.xml when you detach if you had already configured FCM?).
  3. Delete the GCM service declarations in AndroidManifest.xml.
  4. Set AppConstants.FCM_ENABLED = true in AppConstants.java.

To test sending to a specific device, call Expo.Notifications.getDevicePushTokenAsync() to get your FCM token, then feed that into the cloud messaging tester on the Firebase console.

2 Likes
closed #4

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.