Integrate Firebase In-App Messaging in React Native for User Engagement

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 init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run

npm install -g react-native-cli

Run the following commands to create a new React Native project

react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

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

To run the project on an Android Virtual Device or on real debugging device

react-native run-android

or on the iOS Simulator by running (macOS only)

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.

react_native_firebase_in_app_messaging1

Click on “Create your first campaign” and you will see following options to create a campaign.

react_native_firebase_in_app_messaging2

react_native_firebase_in_app_messaging3

react_native_firebase_in_app_messaging4

react_native_firebase_in_app_messaging5

react_native_firebase_in_app_messaging6

react_native_firebase_in_app_messaging7

react_native_firebase_in_app_messaging8

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.

react_native_firebase_in_app_messaging9

react_native_firebase_in_app_messaging10

Download Source Code

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. 🙂

2 thoughts on “Integrate Firebase In-App Messaging in React Native for User Engagement”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.