React Native Firebase In-App Messaging
This is our fourth post of React Native Firebase series, in this example we will see what is Firebase In-App Messaging? and how to integrate In-App Messaging in React Native App? How to keep your App users engaged with interesting campaigns.
So Let’s start with the Firebase In-App Messaging Example.
Firebase In-App Messaging
Firebase In-App Messaging helps you to engage your apps active users by sending them targeted, contextual messages that encourage them to use key app features.
For example, you could send an in-app message to get users to subscribe, watch a video, complete a level, or buy an item. You can customize messages as cards, banners, modals, or images, and set up triggers so that they appear exactly when they’d benefit your users most.
React Native Firebase provides support for both native Android & iOS. Note: Display messages are different from the Firebase notifications.
Enable or Disable Display Messages
There can be some situations where we don’t want some users to receive the In-App Messages. Let say, you want to show messages to registered users only or to unpaid/free subscribers only then you can disable the subscription using setMessagesDisplaySuppressed method.
const allowToReceiveMessage = (isAllowed) => {
setCanReceiveMessage(isAllowed);
};
The suppressed state is not persisted between restarts, so ensure it is called as early as possible.
Example Description
In this example we will create a simple app where we will have a button to enable or disable the display message. By clicking on the button he/she can suppress the display message. Once we install our React Native App we will create a campaign from Firebase Console which will show the display message on next app launch. So let’s get started with the code and then you will see the steps to create campaign.
To Make a React Native App
Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react native command line interface to make our React Native App.
If you have previously installed a global react-native-cli package, please remove it as it may cause unexpected issues:
npm uninstall -g react-native-cli @react-native-community/cli
Run the following commands to create a new React Native project
npx react-native init ProjectName
If you want to start a new project with a specific React Native version, you can use the --version argument:
npx react-native init ProjectName --version X.XX.X
Note If the above command is failing, you may have old version of react-native
or react-native-cli
installed globally on your pc. Try uninstalling the cli and run the cli using npx.
This will make a project structure with an index file named App.js in your project directory.
Integration of Firebase SDK
For starting with any of the React Native Firebase Example you will need to integrate Firebase in your app, I have specially made a separate post in detail for this where you will see point to point process to add Firebase in your React Native App for Android and iOS both.
Please visit How to Integrate Firebase in Android and iOS App and come back for the next step.
Once you are done with the Firebase integration you can install the further dependencies.
Installation of Dependencies
To install the dependencies open the terminal and jump into your project using
cd ProjectName
For the React Native Firebase we need to install and setup the app module
npm install @react-native-firebase/app --save
Now install the in-app-messaging module
npm install @react-native-firebase/in-app-messaging --save
This command will copy all the dependencies into your node_module directory. –save is optional, it is just to update the dependency in your package.json file.
CocoaPods Installation
After the updation of React Native 0.60, they have introduced autolinking so we do not require to link the library but for iOS we need to install the pods. So to install the pods use
cd ios/ && pod install --repo-update && cd ..
Code to Integrate In-App-Messaging in React Native
Please open App.js in any code editor and replace the code with the following code
App.js
// #4 Integrate Firebase In-App Messaging in React Native for User Engagement
// https://aboutreact.com/react-native-firebase-in-app-messaging/
// Import React in our code
import React, { useState } from "react";
// Import all the components we are going to use
import {
SafeAreaView,
StyleSheet,
View,
Text,
TouchableOpacity,
} from "react-native";
import inAppMessaging from "@react-native-firebase/in-app-messaging";
const App = () => {
const [
canReceiveMessage,
setCanReceiveMessage,
] = useState(true);
const allowToReceiveMessage = async (isAllowed) => {
setCanReceiveMessage(isAllowed);
// Allow/Disallow user to receive messages
await inAppMessaging().setMessagesDisplaySuppressed(
isAllowed
);
};
return (
<SafeAreaView style={styles.container}>
<Text style={styles.titleText}>
Integrate Firebase In-App Messaging{' '}
in React Native for User Engagement
</Text>
<View style={styles.innerContainer}>
<Text style={styles.simpleText}>
User Can Receive Message :{" "}
{canReceiveMessage ? "Yes" : "No"}
</Text>
<TouchableOpacity
activeOpacity={0.5}
style={styles.buttonStyle}
onPress={() =>
allowToReceiveMessage(!canReceiveMessage)
}
>
<Text style={styles.buttonTextStyle}>
{canReceiveMessage
? "Disable Receiving Message"
: "Enable Receiving Message"}
</Text>
</TouchableOpacity>
</View>
<Text style={styles.footerHeading}>
React Native Firebase In-App Messaging
</Text>
<Text style={styles.footerText}>
www.aboutreact.com
</Text>
</SafeAreaView>
);
};
export default App;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
},
innerContainer: {
flex: 1,
alignItems: "center",
padding: 35,
justifyContent: "center",
},
titleText: {
fontSize: 20,
fontWeight: "bold",
textAlign: "center",
padding: 20,
},
simpleText: {
fontSize: 16,
textAlign: "center",
marginVertical: 16,
},
buttonTextStyle: {
color: "white",
fontWeight: "bold",
},
buttonStyle: {
alignItems: "center",
backgroundColor: "orange",
padding: 10,
width: "100%",
marginTop: 16,
},
footerHeading: {
fontSize: 18,
textAlign: "center",
color: "grey",
},
footerText: {
fontSize: 16,
textAlign: "center",
color: "grey",
},
});
To Run the React Native App
Open the terminal again and jump into your project using.
cd ProjectName
1. Start Metro Bundler
First, you will need to start Metro, the JavaScript bundler that ships with React Native. To start Metro bundler run following command:
npx react-native start
Once you start Metro Bundler it will run forever on your terminal until you close it. Let Metro Bundler run in its own terminal. Open a new terminal and run the application.
2. Start React Native Application
To run the project on an Android Virtual Device or on real debugging device:
npx react-native run-android
or on the iOS Simulator by running (macOS only)
npx react-native run-ios
Compose Campaign
Once you installed the App in your device we can create our first campaign from Firebase Console. Just open the Firebase Console and click In-App Messaging tab from left pane.
Click on “Create your first campaign” and you will see following options to create a campaign.
Once you publish your campaign and opens the app you will see a display message. On click of Thanks display message will disappear and will not be visible again.
This is how you can increase user engagement by integrating Firebase In-App Messaging in React Native App for Android and iOS. If you have any doubts or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!
Hope you liked it. 🙂
hello Can please make video on firebase push notification
Working on it
when i install this plugin my build failed how to solve
Thanks!
It is very useful for me.
hi, did you make a video to click on notification go to a perticular page..