## Installation - Run `npm install react-native-fcm --save` - Run rnpm link ## Android Configuration - In `android/build.gradle` ```gradle dependencies { classpath 'com.android.tools.build:gradle:2.0.0' classpath 'com.google.gms:google-services:3.0.0' // <- Add this line ``` - In `android/app/build.gradle` ```gradle apply plugin: "com.android.application" apply plugin: 'com.google.gms.google-services' // <- Add this line ... ``` - In `android/app/src/main/AndroidManifest.xml` ``` ... ... ``` ### IOS Configuration install pod 'Firebase/Messaging' in AppDelegate.m add ``` #import "RNFIRMessaging.h" ... - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { .... [FIRApp configure]; <-- add this line } //add this - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification { [[NSNotificationCenter defaultCenter] postNotificationName: FCMNotificationReceived object:self userInfo:notification]; } //add this - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler { [[NSNotificationCenter defaultCenter] postNotificationName:FCMNotificationReceived object:self userInfo:notification]; handler(UIBackgroundFetchResultNoData); } ``` ### FCM config file In [firebase console](https://console.firebase.google.com/), you can get `google-services.json` file and place it in `android/app` directory and get `googleServices-info.plist` file and place it in `/ios` directory ### Usage ```javascript var FCM = require('react-native-fcm'); componentWillMount() { FCM.requestPermissions(); FCM.getFCMToken().then(token => { //store fcm token in your server }); this.fcmNotifLsnr = DeviceEventEmitter.addListener('FCMNotificationReceived', (notif) => { //there are two parts of notif. notif.notification contains the notification payload, notif.data contains data payload }); this.fcmTokenLsnr = DeviceEventEmitter.addListener('FCMTokenRefreshed', (token) => { //fcm token may not be available on first load, catch it here }); } componentWillUnmount() { //prevent leak this.fcmNotifLsnr.remove(); this.fcmTokenLsnr.remove(); } } ``` ### Pass `click_action` and `data` when sending notification When app is not running when user clicks notification, notification data will be passed into - `FCM.initialAction`(contains `click_action` in notification payload - `FCM.initialData` (contains `data` payload if you send together with notification) When app is running in background - IOS will receive notificaton from `FCMNotificationReceived` event - Android will reload the whole react app (I'm looking for suggestions to fix that) When app is running in foreground - Both will receive notificaton from `FCMNotificationReceived` event ## Q & A ### My android build is failing Try update your SDK and google play service ### I can't get data notification when app is killed? If you send notification with payload only, you can only get the data message when app is in foreground or background. Killed app can't show notification ### I can't get data notification when app is in background with hybrid notification I want to figure it out too. Looks like that is how GCM/FCM works. I'm sending 2 notification separately for now. Let me know if you find a better solution ### I can't get notification payload when my android app is in foreground It is better to use data payload if you need to pass data into application. I haven't implemented notification payload bridging in android module and am not planning to (the SDK should handle the payload for you) ### Notification payload and data payload are mixed in iOS app. I'm not doing any filtering. Try to add some `type` attributes to differentiate data payload from APN notification ### It is missing some features Issues and pull requests are welcomed. Let's make this thing better!