expo-firebase-messaging
expo-firebase is still in RC and therefore subject to breaking changings. Be sure to run
yarn upgrade
andcd ios; pod install
when upgrading.
expo-firebase-messaging
enables cloud messaging (FCM) in your app.
Installation
First, you need to install the package from npm
registry.
npm install expo-firebase-messaging
or yarn add expo-firebase-messaging
iOS
Cocoapods
If you're using Cocoapods, add the dependency to your Podfile
:
pod 'EXFirebaseMessaging', path: '../node_modules/expo-firebase-messaging/ios'
and run pod install
.
Common Setup
Upload certs to Firebase
You can reliably produce a .p12
file with fastlane.
- In
ios/
runfastlane produce
- This will create an entry in the App Store. This method can cut down on reserved bundle ID errors. - While still in
ios/
runfastlane pem
- This will generate the.p12
file and provide you with a local path to the file. - You should see something like this - copy the middle file to your Firebase project's settings. You can use it as a development key if needed.
Private key: /Users/you/Documents/yourapp/ios/production_com.company.yourapp.pkeyp12 certificate: /Users/you/Documents/yourapp/ios/production_com.company.yourapp.p12PEM: /Users/you/Documents/yourapp/ios/production_com.company.yourapp.pem
Android
-
Append the following lines to
android/settings.gradle
:include ':expo-firebase-messaging'project(':expo-firebase-messaging').projectDir = new File(rootProject.projectDir, '../node_modules/expo-firebase-messaging/android')and if not already included
include ':expo-core'project(':expo-core').projectDir = new File(rootProject.projectDir, '../node_modules/expo-core/android')include ':expo-firebase-app'project(':expo-firebase-app').projectDir = new File(rootProject.projectDir, '../node_modules/expo-firebase-app/android') -
Insert the following lines inside the dependencies block in
android/app/build.gradle
:api project(':expo-firebase-messaging')and if not already included
api project(':expo-core')api project(':expo-firebase-app') -
Update the manifest (
android/app/src/main/AndroidManifest.xml
):<!--Inlcude this for Background Messages--> -
Include the module in your expo packages:
./android/app/src/main/java/host/exp/exponent/MainActivity.java
/** At the top of the file.* This is automatically imported with Android Studio, but if you are in any other editor you will need to manually import the module.*/// This should be here for all Expo Firebase features.// Later in the file...@Overridepublic List<Package> {// Here you can add your own packages.return Arrays.<Package>;}
Usage
;;;; ; // API can be accessed with: firebase.messaging(); Component state = user: null ; async { const status = await Permissions; if status !== 'granted' return; thismessageListener = firebase; // Get device push token const token = await firebase; } { // Clean up: remove the listener this; } { return <View />; }
You can test sending messagings to your app by executing the following command in a terminal:
curl -X POST --header "Authorization: key=FIREBASE_SERVER_KEY" --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"to\":\"DEVICE_TOKEN_ID\",\"message\":{\"body\":\"Test\"}}"
Just be sure to populate the command with your firebase server key and the device token ID from firebase.iid().getToken()
Listen for FCM messages in the background-(Optional)(Android-only)-Listen-for-FCM-messages-in-the-background)
Android allows you to act on data-only messages when your application is closed or running in the background. This is particularly useful if you'd like to be able to show heads-up notifications to your user.
-
Ensure your manifest has the following service registered (
android/app/src/main/AndroidManifest.xml
):<!--Inlcude this for Background Messages--> -
Create a task handler
// @flow;// Optional flow type;{// handle your messagereturn Promise;};This handler method must return a promise and resolve within 60 seconds.
-
Register the background handler in your Expo app (
App.js
);;AppRegistry;The name
"FirebaseBackgroundMessage"
is very important.