Using Firebase for push notifications in iOS

Nabin Shrestha
3 min readJan 14, 2021

In this tutorial we are going to implement push notification in our iOS app using firebase.

For this tutorial, we are going to need:

  1. Postman
  2. Xcode
  3. Good Understanding on cocoapods.

Step One:

Create a new Xcode project and integrate cocoapods in it. In Podfile, add:

pod ‘Firebase/Messaging’

pod ‘HandyJSON’

Run pod install and build the project.

Step Two:

Go to Signing & Capabilities in project and add capability named Push Notifications.

Step Three:

In AppDelegate, import Firebase and inside didFinishLaunchingWithOptions function, add:

FirebaseApp.configure()

Step Four:

Create a new project in https://console.firebase.google.com/ for your app. Add an iOS app in that project. You will be provided with a file named GoogleService-Info.plist. Add that file in project and make sure Xcode has reference to that file.

Note: You have to upload .p8 file in cloud messaging tab to be able to send notifications. You can create and download that file from your apple developer account website.

Step Five:

Create a swift file named PushNotificationManger.swift, that we will use to handle the push notifications in our app.

Create a struct named FirebaseNotificationAPI.swift, we will be converting the response obtained from FCM to a object for later use.

Step Six:

Now register our PushNotificationManger class in AppDelegate.swift didFinishLaunchingWithOptions func.

Step Seven:

Now run the app. If everything went smoothly, FCM Token will be printed in the debugger. We’ll use it in Postman.

Now in Postman, choose POST method and use URL https://fcm.googleapis.com/fcm/send to send FCM.

In Headers, we’ll add Authorization and Content-Type.

Go the the firebase console and select project and go to project settings. In cloud messaging app there is a server key. Copy it and paste in in postman Authorization value like this:

key=<YOUR SERVER KEY>

and Content-Type will be application/json

It should look like following screenshot:

FCM Authorization and Content-Type

Now, select the Body tab in Postman and select the raw radio button there. In the far right there is Text selected by default. Choose JSON from that dropdown.

Paste the following content in the body section.

Inside registration_ids array, replace with your FCM Token printed in the Xcode debugger.

Then hit Send.

Now your should see a notification in your device like:

FCM Push Notification banner
FCM Push Notification banner in home

Note: This is the vanilla design notification that is shown by the system. We can also show design with image to the right like below.

I will be writing the tutorial on how to make this in the next one.

--

--