Tutorial

Implementing Push Notifications on iOS with Firebase


Push Notifications is a loud and powerful way for our apps to engage with our users. We engage with our users by letting them see values with their very owns eyes. Users see values. Users engage. We can engage our users by letting them know about something important. Perhaps their favorite sport team is about to compete. A flash sale on watermelons is on for 30 minutes. Or there is an important meet up in the desert later this weekend. These are some scenarios when our users may want to be notified.

One way to implement push notifications for iOS apps is through Firebase Notifications.

In this tutorial, we will dive into Firebase Notifications. We will see how we can implement this feature into our Xcode project one step at a time. This tutorial is split into 7 fine looking sections:

  • Apple Developer Account Configuration
  • Generating a CSR file
  • Uploading your CSF file
  • Preparing the APNs Certificate
  • Configuring Firebase for Push Notifications
  • Building the Firebase Notification Demo App
  • Testing Push Notifications

I will try to be as clear and concise as possible. Hope you enjoy the article and let’s get started!

Configuring Your Apple Developer Account

The first step is to have a paid Apple developer account. Yes, you need to enroll into the Apple Developer Program ($99 per year) to unlock the push notifications capability.

Assuming you already got a paid developer account, go ahead and login to your Apple Developer account. Once logged in, you will be re-directed to the Apple Developer homepage. From there, you should see “Account” at the top navigation bar. Click that option.

Now you should be inside of your Apple Developer account.

firebase-notification-apple-developer

Now look to the left side bar. The third row should say “Certificates, IDs & Profiles.” Select that option.

firebase-notification-certificate-option

Now you are in the “Certificates, Identifiers & Profiles” page.

firebase-notification-certificate-profile

Look to left side bar and there should be a section called “Identifiers.” Under that section, there is a link that says “App IDs.” Press that.

firebase-notification-app-id

You should see all your iOS App IDs.

firebase-notification-app-id-2

Now at the top right, you should see a + button. Press that. After that, you should be at this stage:

firebase-notification-app-id-3

We now need to fill out the followings:

  • App ID Description — Name. Here, you should put your app’s name (e.g. Firebase Notification Demo)
  • App ID Suffix — Explicit App ID — Bundle ID. Here, you need to select a unique bundle identifier for your app (e.g. com.appcoda.firebasenotificationsdemo). Please make sure you use your own bundle ID instead of using mine.

Then under App Services, tick “Push Notifications.” Press continue.

After that, you will be redirected to a “Confirm your App ID” page. Press register.

Now we are back to our “iOS App IDs” page. Look for the App ID you just created. Press on it and you should see a a drop down of Application Services.

Scroll down until you reach the end of the drop down and you should see an “Edit” button. Press that.

firebase-notification-push-enabled

The “iOS App ID Settings” page will show up.

firebase-notification-push-setting

Scroll all the way down until you see “Push Notifications.”

It is time for us to create a “Client SSL Certificate.” This will allow our notification server (Firebase) to connect to the Apple Push Notification Service. Under Development SSL Certificate, press on the “Create Certificate…” button.

firebase-notification-push-ssl

Now we will see this.

firebase-notification-push-ssl-2

To generate a certificate, we would need a Certificate Signing Request (CSR) file from our Mac. We will get back to this page later, but now we need the CSR file.

Generating a CSR file

To generate a CSR file, press cmd + space and do a spotlight search for “Keychain Access.” Open Keychain Access, and go up to the menu to select Keychain Access > Certificate Assistant > Request a Certificate From a Certificate Authority…

firebase-notification-keychainaccess

A “Certificate Assistant” pop up should appear.

firebase-notification-cert-assistant

Fill in your email address and name. Choose “Saved to disk” and press Continue. Then save your CSR somewhere on your hard drive.

Uploading Your CSR File

Now that we have our CSR generated, it is ready to go back to the “Add iOS Certificate” page.

Adding CSR

Scroll down. Press continue, and then click “Choose file…” Select the CSR file you just saved on your hard drive.

add-csr-choose-file

Next, click continue again. Then the web page should say “Your certificate is ready.”

add-csr-ready

Now you can go ahead and click on the blue download button to download your certificate.

Preparing the APNs Certificate

Now that you have created the iOS certificate, we will then prepare the APNs (short for Apple Push Notifications) certificate, which will be used later in the Firebase configuration. Open up Finder and locate the certificate you downloaded earlier.

firebase-locate-cert

Double click the certificate file (e.g. aps_development.cer) to add the certificate into Keychain Access.

Now open up Keychain Access. Under the “My Certificates” category, you should see the certificate you just added. It should be called:

Click the expand arrow to the left of the certificate’s name to reveal the private key option. Right click on the private key and press export.

apns-export-cert

Once clicking Export, a pop up will prompt you to save your private key as a .p12 file. Go ahead and click save.

apns-export-cert

Then enter a password if you want to protect your exported certificate. Click OK to confirm.

apns-export-cert

Awesome! We just completed the configuration and preparation. Now we are ready to move onto Firebase! Let’s get started.

Configuring Firebase for Push Notifications

First, head over to Firebase console. Sign in with your Google account to enter the console. If you don’t know how to switch to the console, there is a button at the very top right that says “Go to console.” Go ahead and click on that.

firebase-console

Once you are at the console, click on the “CREATE A NEW PROJECT” button.

firebase-new-proj

I name my project “Firebase Notification Demo” but you’re free to use other names. Simply click the “CREATE A NEW PROJECT” button to proceed.

firebase-name-project

After that, you are redirected to the project overview page. Now click the “Add Firebase to your iOS” button. Enter your iOS bundle ID. Then click the “ADD APP” button.

firebase-add-app

Follow the on-screen instruction to download the GoogleInfo.plist file. We are going to need this file later.

firebase-download-plist

Click “continue” to go to the next step. You will see instructions showing you how to add the Firebase SDK into our project. I will walk you through how to configure the SDK later. For now, just ignore and click “Continue” to proceed.

firebase-install-sdk

Finally click “Finish” to complete the configuration. You should then see your iOS app in the Firebase overview page.

firebase-overview

Look for the setting icon at the top right. Click the settings icon > Project settings.

firebase-project-settings

Select the Cloud Messaging tab.

firebase-cloud-messaging

Scroll down, and click the “Upload Certificate” button.

firebase-cloud-messaging-2

Then a pop should appear to ask you for your Development APNS certificate.

firebase-apns-cert

Click browse and choose your APNs certificate (i.e. .p12 file) that you prepared in the earlier section. If you configured the file with a password, enter certificate password, followed by clicking the Upload button.

apns-export-cert-upload

Now you should see a Development APNs certificate file in the Cloud Messaging setting.

apns-export-cert-upload-2

For this demo, we are going to use the Development APNs certificate. If you do decide to put your app onto the App Store, make sure that you have a Production APNs certificate file for Firebase as well.

Great! We finally finish all the configurations. It’s time to head back to Xcode to create the demo.

Building the Firebase Notification Demo App

Now fire up Xcode and create a new project using the Single View Application template. Name your Xcode project whatever you want.

firebase-demo-new-proj

Please note that your bundle ID should be identical to the one you set in your Apple Developer dashboard. If you already forgot the ID, you can go back to App IDs under Identifiers and see what it is. In case you use a wrong ID, you will not be able to receive any push notifications.

Installing the Firebase SDK Using CocoaPods

Next, it is time for us to integrate the Firebase SDK into our Xcode project. The simplest way to do that is by using CocoaPods. First, close the Xcode project and then openTerminal. Change to the directory of your project and run the following command to initialize your Podfile:

Open the Podfile, add the following pods to your Podfile:

Save it. Your podfile should look something like this:

Go back to your terminal and run the following command to install the pods:

CocoaPods will then download the SDK for you and create a .xcworkspace file for you. Make sure you open that file in Xcode.

Editor’s note: If you are new to CocoaPods, please check out our beginner’s guide to CocoaPods.

Adding GoogleService-Info.plist

With the SDK installed, it’s time to add the GoogleService-Info.plist file that you have downloaded before. Drag that file from Finder to your project folder.

Once it is done, you should see the GoogleService-Info.plist in your project folder.

Note: After adding the GoogleService-Info.plist file to your Xcode project, please check the bundle ID of your project. Make sure the bundle ID is the same as you configured before. This is very important as your app will not receive push notifications if this bundle ID is wrongly set.

Enabling Push Notifications

Cool! The next step is to enable the Push Notifications capability of the demo project. Select the project in the project navigation, and then choose Target > Capabilities. Flip the switch of Push Notifications to ON to enable it.

demo-add-push-notification

Initializing Push Notifications

It’s time to write some code and test push notifications. Head over to AppDelegate.swift and import the following modules:

Adopt the following protocols in your App Delegate class:

Next, implement the applicationReceivedRemoteMessage required method of the FIRMessagingDelegate protocol like this:

Now inside of your application(_:didFinishLaunchingWithOptions:) method, add the following code:

This sets up push notification settings based on your iOS version. The last line FIRApp.configure() is to initialize and configure Firebase. The AppDelegate class should look something like this:

firebase-appdelegate

Once you have your App Delegate set up, you can run your Xcode project on your device. Make sure you tap Allow when your app asks you for notifications permission.

demo-allow

Great! You are good to go, and let’s try to send some notifications!

Testing Firebase Notifications

Alright, it’s time to do some tests and try to send some notifications. Go back to the Firebase Console, and then select your Firebase project. Look to the left side bar and you should see the Notifications option. Click the button. Once you see the Notification page, click the “SEND YOUR FIRST MESSAGE” button.

Firebase Notifications Option

After so much work, it is about time that we send “the best push notification ever” from Firebase. Let’s go ahead and do that.

Fill in the “Message text” field with a message you would like your users to see. Then select your app under the Target section.

Sending Firebase Notifications

Scroll down and press the “SEND MESSAGE” button.

Sending push notifications

A pop up will ask you to review your notification message. Press the SEND button to send out your notification.

reviewing push notification

If you lock your device, you should be able to see the notification you pushed on your lock screen. You can then swipe from left to right on the notification to open the app. If you are on your home screen, you will a notification message drop down from the top of your screen. You can tap on the notification to open the app.

Notifications on lock screen

That’s it!

Wrap Up

We have implemented Firebase Notifications into our Xcode project. We took it one step at a time and made to the end. Congratulations for making it this far!

I hope you have enjoyed and learned something valuable from my article. If you have, please let me know by sharing this article with your friends.

SwiftUI
Detecting Scroll Positions in ScrollView with SwiftUI
Tutorial
The Complete Guide to Creating Your Own CocoaPods in Swift
macOS
Using Packages to create an installer for distributing macOS apps outside of the Mac App Store
Shares