AirBnB Clone with React Native Part 13: Firebase Push Notifications for iOS and Android
Tags: programming, firebase, mobiledevelopment, heartbeat
Published: 2020-02-10 15:06:56

---
title: AirBnB Clone with React Native Part 13: Firebase Push Notifications for iOS and Android
published: true
date: 2020-02-10 14:51:19 UTC
tags: programming,firebase,mobile-development,heartbeat
canonical_url: https://heartbeat.fritz.ai/airbnb-clone-with-react-native-part-13-firebase-push-notifications-for-ios-and-android-7b0d4a29fc49
cover_image: https://cdn-images-1.medium.com/max/1024/1*iIEhgq6z5aE3Ipn7Ejz8GQ.jpeg
---


This is the eleventh chapter of our implementation of an [Airbnb clone in React Native](instamobile.io), we’ll set up push notifications for our AirBnB clone in iOS and Android. With the use of Firebase, we can easily complete this task. Before the implementation, however, we have to configure our apps to enable push notifications.

To catch up on the other parts of this series, [head over to my Medium profile](https://heartbeat.fritz.ai/search?q=krissanawat%20kaewsanmuang), where you’ll find tutorials 1–12.

> Looking for new ways to elevate your mobile app’s user experience? [With Fritz AI, you can teach your apps to see, hear, sense, and think. Learn how and start building with a free account.](https://www.fritz.ai/product/standard.html?utm_campaign=buildapps9&utm_source=heartbeat)

### Set up Push Notifications for iOS

You need an Apple developer account, which costs 100$ a year, for this implementation. If you don’t have a developer account, don’t worry, you can skip to the Android implementation of push notifications.

#### Configuring APNs with FCM

We need an Apple developer certificate or key to allow Firebase to send a notification message to APNs (Apple push notification service). To acquire a certificate or key, go to the Apple developer dashboard.

![](https://cdn-images-1.medium.com/max/1024/0*uoXUCpN7zAjKF6Jk.png)

#### Create the authentication key

This section describes how to generate an authentication key for an app ID enabled for push notifications. If you have an existing key, you can use that key instead of generating a new one.

To create an authentication key:

1. In your developer account, go to Certificates, Identifiers & Profiles, and under Keys, select All.

2. Click the Add button (+) in the upper-right corner.

![](https://cdn-images-1.medium.com/max/1024/0*zTuc26h7Llhf2cO8.png)

3. Enter a description for the APNs Auth Key. Under Key Services, select the APNs checkbox, and click Continue.

![](https://cdn-images-1.medium.com/max/1024/0*Lfv9rYRxZoZ0C2a2.png)

4. Click Confirm and then Download. Save your key in a secure place. This is a one-time download, and the key cannot be retrieved later.

![](https://cdn-images-1.medium.com/max/1024/0*Jnv4v3BrjBOpB0v0.png)

5. If you’d like to verify that your APNs authentication key is set up properly and is accepted by APNs, try sending a test push notification.

#### Add the Certificate to Firebase

Go to [cloud messaging setting](https://console.firebase.google.com/u/0/project/airbnbclone-b96af/settings/cloudmessaging/ios:org.reactjs.native.example.airbnbclone) to add the certificate to Firebase.

Upload the certificate to the AirBnB clone app and add the Key ID as well as the Team ID.

![](https://cdn-images-1.medium.com/max/982/1*LdHMZGdl6jHzMjmuyZPuRA.png)

#### Configure the Project in Xcode

Open our project with Xcode and select the +Capability option inside Signing & Capabilities. Search for the push notification option and select it. In the same way, search for background modes. Select the Remote Notifications option from the listed options.

![](https://cdn-images-1.medium.com/max/903/1*-1dXancis_A09iK9MM0MnA.png)

If you have followed the steps correctly, you will be able to see the following window.

![](https://cdn-images-1.medium.com/max/735/1*5M5AkKoPYZPG3heMR8pjVg.png)

As the next step, include Firebase messaging in the Podfile

{% gist https://gist.github.com/krissnawat/da326d39d6ee9519fe186995a46a964d %}

We’ll now return to Xcode and open the Appdelegate.m file. To activate messaging on Appdelegate.m, first import the Firebase Notification library.

```
#import "RNFirebaseNotifications.h"
```

We activate the lib by adding a code segment below the Firebase lib:

```
if ([FIRApp defaultApp] == nil) {
    [FIRApp configure];
    [RNFirebaseNotifications configure];
  };
    [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
  return YES;
}
- (void)application:(UIApplication \*)application didReceiveLocalNotification:(UILocalNotification \*)notification {
  [[RNFirebaseNotifications instance] didReceiveLocalNotification:notification];
}
```

We’ve now completed the push notification set up. Close the packager terminal and run Xcode build once again to add the new functionality to the app.

> Looking for a spark to take your apps to the next level? Machine learning enables powerful, highly-personalized mobile experiences. [Subscribe to the Fritz AI Newsletter to learn how.](https://www.fritz.ai/newsletter?utm_campaign=fritzai-newsletter-scale4&utm_source=heartbeat)

### Set up Push Notifications for Android

You can also use the React Native Firebase package to set up push up notifications for Android following these steps.

**Step 1** : Add a dependency to android/app/build.gradle file:

{% gist https://gist.github.com/krissnawat/173e74c49beff1e199eb2da0d942f735 %}

**Step 2** : Import and activate the package in MainApplication.java:

```
import io.invertase.firebase.messaging.RNFirebaseMessagingPackage;
```

Add the following line of code to activate the package:

```
packages. **add** (new **RNFirebaseMessagingPackage** ());
```

You can see the combined implementation in the following example:

{% gist https://gist.github.com/krissnawat/5779e6ec0e861b00ab94b42265c2e8fe %}

**Step 3:** Configure AndroidManifest

We will add the messaging service to the application component inside the manifest file, which resides in the following path: android/app/src/main/AndroidManifest.xml

{% gist https://gist.github.com/krissnawat/16bdf472c2021c8083fc820d3375e65f %}

Your manifest file should now have the following format:

{% gist https://gist.github.com/krissnawat/10a14d808a5a9d127a12e65666e8a50b %}

Import the Firebase module to App.js to verify the device token.

import firebase from react-native-firebase

Check whether the device has already given messaging permission.

{% gist https://gist.github.com/krissnawat/d6362e3fbe789203be9878002faf631a %}

If the user doesn’t have permission, request permission from the user.

{% gist https://gist.github.com/krissnawat/64121f62c2a72fa4ee761850609402f2 %}

If the user accepts the request, retrieve the token.

{% gist https://gist.github.com/krissnawat/b8beeab039927de29720906b85c4fe4a %}

Then reload the app.

![](https://cdn-images-1.medium.com/max/576/1*BIzjz0yiAD2qT_E6_vOHSA.png)

We can see the generated token on the Firebase console.

![](https://cdn-images-1.medium.com/max/1024/1*ct0JVZubwpInMBUEGJnr2A.png)

In Android, once you rebuild the app again, permission will be enabled by default.

Let’s try sending a test message now. First, go to the Firebase [cloud messaging console](https://console.firebase.google.com/u/0/project/airbnbclone-b96af/notification/compose).

![](https://cdn-images-1.medium.com/max/973/1*U8baeZrlnMa_GYvJOJGQuA.png)

Press “Send test message”.

The test notification will appear on the mobile screen. In iOS:

![](https://cdn-images-1.medium.com/max/621/1*4YBkjOqvyXlDLKNBKcj8ZQ@2x.jpeg)

And in Android:

![](https://cdn-images-1.medium.com/max/423/1*VPTp6O4wmQkrTpWqaSEXXQ.png)

### Summary

Today, we learned how to set up Firebase push notifications on iOS and Android. We also set up an APNS certificate on Firebase when setting up push notifications for iOS. In the next chapter, we’ll store the token in a real-time database and then, using cloud functions, fetch the token and send it to the subscribed device.

_Editor’s Note:_[**_Heartbeat_**](http://heartbeat.fritz.ai/) _is a contributor-driven online publication and community dedicated to exploring the emerging intersection of mobile app development and machine learning. We’re committed to supporting and inspiring developers and engineers from all walks of life._

_Editorially independent, Heartbeat is sponsored and published by_[**_Fritz AI_**](http://fritz.ai/)_, the machine learning platform that helps developers teach devices to see, hear, sense, and think. We pay our contributors, and we don’t sell ads._

_If you’d like to contribute, head on over to our_[**_call for contributors_**](https://heartbeat.fritz.ai/call-for-contributors-october-2018-update-fee7f5b80f3e)_. You can also sign up to receive our weekly newsletters (_[**_Deep Learning Weekly_**](https://www.deeplearningweekly.com/) _and_[](https://www.fritz.ai/newsletter)_the_ [**_Fritz AI Newsletter_**](https://www.fritz.ai/newsletter/?utm_campaign=fritzai-newsletter&utm_source=heartbeat-statement)_), join us on_[](https://join.slack.com/t/fritz-ai-community/shared_invite/enQtNTY5NDM2MTQwMTgwLWU4ZDEwNTAxYWE2YjIxZDllMTcxMWE4MGFhNDk5Y2QwNTcxYzEyNWZmZWEwMzE4NTFkOWY2NTM0OGQwYjM5Y2U)[**_Slack_**](http://fritz.ai/slack)_, and follow Fritz AI on_[**_Twitter_**](https://twitter.com/fritzlabs) _for all the latest in mobile machine learning._

* * *