Push notifications on iOS with Firebase

push notifications ios firebase

Reading Time: 11 minutes

Push notification is a message or an information that is “pushed” or delivered from server to client application (iOS application). From its introduction back in 2009., push notifications became more and more powerful and from iOS 10 you can use push notifications for more than sending a short message. Let’s take a look at what you can do with push notifications:

  • Display a short message
  • Play a notification sound
  • Show a badge number on the app’s icon
  • Provide users with actions they can take without opening the application
  • Send a rich push notification (push notification with a media attachment)
  • Send silent notification – this type of push notification will silently trigger your app to wake up in the background and perform some task

In this post, we’ll cover push notifications on iOS 11 with Swift 4 and we’ll use Firebase for sending push notifications. If you’re not familiar with Firebase, simply put it is an application development platform which provides you with many functionalities like cloud database and storage, remote configuration, analytics, user authentication, messaging, crash reporting etc. You can find more information here.

Before starting with push notifications you’ll need the following:

  • Apple developer account and Xcode (which I presume you already have)
  • Application and Client SLL Certificate (on Apple developer portal)
  • Create account and project on Firebase and configure it for push notifications

Table of content:


Create application and Client SSL Certificate on Apple developer portal

Log in to Apple developer portal with your Apple ID, go to “App IDs” option (under Identifiers), enter a name for your application, bundle id and register a new app. It is important that you select Push Notifications option under App Services.


push notifications ios firebase app id

In Firebase push notifications configuration you’ll have 2 options – you can either use an APNs authentication key or APNs certificate to connect with Apple Push Notification Service. Configuration with APNs Authentication Key is a newer method for sending notifications. I’ll show you how to create APNs authentication key and APNs certificate and you decide which option you want to use on Firebase.


Create APNs certificate

After you registered your application, select it, click on “Edit” button and scroll down to Push Notifications option. To send push notifications we’ll need to create Client SSL Certificate which allows notification server (in this case Firebase) to connect to the Apple Push Notification (APN) Service. Click on “Create Certificate” button under Development SSL Certificate.

push notifications ios firebase

You’ll be presented with next screen, Add iOS Certificate.

push notifications ios firebase

Follow instructions on this page but basically, you need to open “Keychain Access” on your Mac, select Keychain Access > Certificate Assistant > Request a Certificate from a Certificate Authority. Enter your email address in the User Email Address field and a name for your private key in the Common Name field (for example John Smith APN Dev Key). Leave The CA Email Address field empty and select option “Saved to disk” under “Request is” group. Click “Continue” within Keychain Access to complete the CSR generating process and save a file somewhere on your disk.

Now go back to Apple Developer portal, click Continue and you’ll be presented with the following screen.

push notifications ios firebase

Upload CSR file you saved on a disk, click Continue and you should see message “Your certificate is ready.”. Download the certificate and double-click on it to add it to Keychain Access. We’ll use this certificate later during Firebase configuration.

Go to Keychain Access, find a newly added private key, right click on it, select an option Export “Key”, enter a password (don’t forget to write this password somewhere) and save your private key as .p12 file on your disk.


Create APNs Authentication Key

On Apple Developer portal, go to Certificates, Identifiers & Profiles, and under Keys, select All. Click the “+” button in the upper-right corner. You’ll be presented with a screen where you need to enter a key description for the APNs Auth Key and select APN’s checkbox under Key Services. Click “Continue”, then “Confirm” button and now you can download your key.

push notifications ios firebase

Note – After downloading your key, it cannot be re-downloaded as the server copy is removed. So save a backup of your key in a secure place.

Provisioning profile

We’ll also need a provisioning profile to run our app on the device so go to “Provisioning Profiles” section on Apple Developer portal and click on option “Development”. Now click on “+” button in the upper-right corner, and on next screen select iOS App Development option.

push notifications ios firebase

After this we need to select our App ID, certificate, devices we want our app to run on, enter a name for provisioning profile and download it.

Next, let’s go through Firebase configuration.


Firebase configuration

Go to Firebase official page and sign in with your Google Account. After that click on “GO TO CONSOLE” and create a new project for your app.

push notifications ios firebase

Now you’ll be redirected to your project overview screen. Here click on “Add Firebase to your iOS app” option. Enter you app bundle ID and optionally App nickname and App Store ID if you want to.

push notifications ios firebase

Click “REGISTER APP” button and follow the instructions on screen – download “GoogleService-Info.plist” file which you’ll add to your project little later and click “Continue”. You’ll be presented with next screen that explains how to add Firebase SDK to your project. You can skip this because we’ll cover this section little later. Click on button “Finish” and we’re done with Firebase project configuration.

Now you’ll see project overview screen where you need to select “Project settings” option.

push notifications ios firebase

Click “Cloud Messaging” and the following screen will be presented.

push notifications ios firebase

As I already said, you have 2 options – you can either use an APNs authentication key or APNs certificate to connect with Apple Push Notification Service. Configuration with APNs Authentication Key is a newer method for sending notifications and in this tutorial, we’ll use this method. So click on upload APN’s auth key, select your .p8 file, enter Key ID (which you can find on Apple developer portal under your key details screen from) and App ID Prefix (which you can also find on Apple developer portal if you go to App IDs and select your app).

push notifications ios firebase

After that click on upload button and we’re done.


Setup Xcode project

Now you can open Xcode and create a new project. For bundle identifier enter the same information you entered on Apple Developer portal when you created a new app ID. When this is done take “GoogleService-Info.plist” that you downloaded earlier from Firebase and add it to your project.

After this, we need to add Firebase SDK to our project. The simplest way to do that is by using some dependency manager (CocoaPods, Carthage). In this article, we’ll use CocoaPods. If you don’t know what CocoaPods is or how to use it you can read more information here.

Open Terminal, go to your project directory and run pod init command to create your Podfile.

When this is done open a podfile, add following lines and save it:

pod 'Firebase/Core'

pod 'Firebase/Messaging'

Your podfile should look like this:

# Uncomment the next line to define a global platform for your project

# platform :ios, '9.0'

#def shared_pods

pod 'Firebase/Core'

pod 'Firebase/Messaging'


target 'PushNotificationsFirebase' do

  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks


  #    shared_pods


Now we need to install the pods and to do this go back to Terminal and enter command pod init – CocoaPods will install desired pods (in this case Firebase SDK) and create .xcworkspace file for your project. If you have a project opened in Xcode close it and reopen it by clicking on .xcworkspace file. Note – from now on, be sure to open xcworkspace file and not a project file.

Next step is to enable push notifications for our project. Go to Xcode, select target, then Capabilities and turn Push Notifications option on.

push notifications ios firebase


And finally, we can dive into code :).


Initialize Firebase and register for remote notifications

We need to initialize Firebase and then register for remote notifications. To do this open AppDelegate.

First, add following lines at the top of the file:

import Firebase

import FirebaseMessaging

import UserNotifications

Besides Firebase we also need to import a new framework, called UserNotifications, which is responsible for the delivery and handling of local and remote notifications.

Now we can configure Firebase by calling following line (typically in application:didFinishLaunchingWithOptions: method):


After this, we need to register our app for remote notifications. To do this call registerForRemoteNotifications method. Create a new method, registerForNotifications and call it in application:didFinishLaunchingWithOptions: method (also you can put it in applicationWillFinishLaunching method).

func registerForNotifications(_ application: UIApplication) {

//        if #available(iOS 10.0, *) {

            // For iOS 10 display notification (sent via APNS)

            UNUserNotificationCenter.current().delegate = self            

            let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]

            UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: { _, _ in })

//        } else {

//            let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)

//            application.registerUserNotificationSettings(settings)

//        }  



Note – I commented out lines for iOS versions older than iOS 10.0 because in this article we’re covering push notifications on iOS 10 and iOS 11 but if you need to support older versions of iOS just uncomment those lines.

In the code above we’re first assigning our delegate to the UNUserNotificationCenter object to receive display notifications. We need to do this before app finishes launching.

Now we’re getting an error “Cannot assign value of type ‘AppDelegate’ to type ‘UNUserNotificationCenterDelegate?’”.

That’s because we need to adopt UNUserNotificationCenterDelegate in AppDelegate class. We’ll fix this now. Add following code to AppDelegate:

extension AppDelegate: UNUserNotificationCenterDelegate {


Start your app on device and if you configured everything properly you should receive an alert (message) to allow push notifications for app.

push notifications ios firebase


Send first notification (short message) with Firebase

Let’s go back to Firebase Console. Find your project on Firebase, open on it, find option “Notifications” and click on it. You’ll be presented with the following screen.

push notifications ios firebase

Click “SEND YOUR FIRST MESSAGE BUTTON”, enter a message for notification, select your app and click button “SEND MESSAGE”.

push notifications ios firebase

That’s it, now you should receive your first push notification.

push notifications ios firebase


Push notification payload

Each push notification contains a payload (that you need to provide) with information and details about how to deliver the notification to the user. The payload is a JSON dictionary object which must contain an aps key, and determines whether the system does any of the following:

  • display a message to the user
  • set a badge on the app’s icon
  • play a sound
  • deliver the notification silently

Also, payload can include custom keys and values. Custom values can use only primitive types such as string, bool, number dictionary and array.

The payload for the previous example (short message) would look something like this:


     "aps" : { "alert" : “Push notification test message" }


In this article, we’re not going to cover this theme in details because with Firebase you don’t need to create payload manually as Firebase handles everything. But you can read more information, like keys and values of apps dictionary here.

As I said at the beginning of this article from iOS 10 you can use push notifications for more than sending a short message. Now I’ll show you how to play a notification sound, set a badge number, and explain what are rich, actionable or silent notifications.


Push notification with sound

You can specify that user should be notified of new push notification by playing a sound. You can use default notification sound or you can specify custom sound which must be included in the app (and not longer than 30 seconds).

Go back to Firebase and click on a button “NEW MESSAGE”. Enter “Push notification with sound” for notification message, select your app, expand “Advanced options”, enable sound and click “SEND MESSAGE”.

push notifications ios firebase

Now you should receive a new push notification with sound. Payload looks like this:


  "aps": {

       "alert": “Push notification with sound”,

       "sound": "default",




Set a badge number on icon

To show a badge number on app icon when your user receives a push notification set a badge key on Firebase to some value (for example 1). Create a new message on Firebase, enable badge and set its value to 1.

push notifications ios firebase


Now you should receive a new notification and if you take a look at your app icon you should see a badge with value 1. Be sure to keep your badges up to date – update your app’s badge (decrement badge counter) as soon as information is read. To remove badge completely set a badge count to 0. You can do this by sending a new push notification with Firebase with iOS badge option enabled and badge count set to 0.

Another way to do this is to handle it in your app.

Let’s go back to Xcode and implement application:didReceiveRemoteNotification:fetchCompletionHandler method. This method tells the app that a remote notification arrived and that you can process it. This will get called whether your app is running in the foreground or in the background.

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

        UIApplication.shared.applicationIconBadgeNumber = 0



Other things you can do with push notifications

In this post, I also wanted to show you some other stuff you can do with push notifications like implementing actions with push notifications, rich push notifications, and silent push notifications but we’ll leave it for some other post. Let me just explain to you what are silent, actionable and rich push notifications.

Rich push notifications

From iOS 10 you can include media attachment to be displayed within push notification. Supported types for media attachment include image, video, and audio. Of course, there are some limitations, like maximum size or supported file types and you can find more information in Apple’s official documentation.

To display a media attachment in push notification you need to use a notification service extension to modify the content before it is delivered to users.

Reason for this is payload size limitation of push notification. As you may already know maximum payload size of a push notification is 4kb so we can’t embed media file in the push notification. Instead, you need to add some new keys in payload – “media-url” (this is URL to your image, video or audio; you can use any key you want just be sure to use the same key in your app) and “mutable-content” key. “mutable-content” key tells OS it should initiate service extension of your app and perform some actions.

We’ll cover this in more details in one of the future posts.

Actionable notifications – provide actions the user can take without opening the app

As you saw in earlier examples when user taps on a notification your app will launch. Instead of the user being forced to launch your app, you can display custom action buttons in the notification which gives users an easy and quick way to perform some task. These notifications are also called actionable notifications.

When user taps on some action button, notification will be dismissed and selected action will be forwarded to your app.

We’ll cover this in more details in one of the future posts.

Silent notifications

Silent notification is a type of notification which allows you to wake up the app and perform some task in the background without notifying the user. This type of notification is not displayed to users.

So you can periodically wake up your app and refresh some data in the background so when a user opens the app next time he’ll have all the latest data.

To configure the notification as silent you must include a content-available key with a value of 1 in notification payload.

Note – when a background update is triggered via silent notification and iOS wakes up your app in the background you have 30 seconds to perform some operation.

We’ll cover this in more details in one of the future posts.


Wrap up

So as you see from iOS 10 you can do several cool things with push notifications except just showing a short text message. Also if you don’t have your own server or don’t want to bother with this you can use Firebase for sending push notifications. It is easy to setup and easy to use. The only problem I had with it is when I wanted to send rich push notifications or push notification with media attachment. No matter what I did it just didn’t work (and the problem is not with my code because it is working with some other solutions).

As I already said, in this post, I also wanted to cover some other stuff, like implementing actions with push notifications, rich push notifications, and silent push notifications but we’ll leave it for some other post.

I hope this article was helpful and useful and that you learned something. If it was please share this article with your friends. And if you have any questions or problems leave me a comment or send me an e-mail.


Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *