iOS Tutorials: iOS Push Notification via Firebase

OVERVIEW

We will be creating an iOS Project that allow Push Notification to your devices by using Firebase. Push Notification is one of the most common feature implemented on apps where you will be able to reach to your users faster with the Push Notification if they decide to turn on notifications.

FIREBASE CONFIGURATION

Go to Firebase and create your Firebase Project and do the necessary step of downloading the Google.plist and Cocoapods. If you don’t know how to do that, rest assured and refer to one of my tutorial on how to set up Firebase. When you are done with the step, come back here again so we can continue creating our push notification with Firebase.

Remember to add the following pods.

CREATE CERTIFICATE

Go to Developer Apple and go to Account and login with your Apple ID and click Certificates, Identifiers & Profiles. The first thing that we are going to do is to create App IDs and click on + button on the top right.

Once you click +, make sure you copy the following information to the respective places and tick Push Notifications on your way down before proceeding to register the App ID.

It should look like below, if you have enable the Push Notifications.

Now, we are going to create iOS Certificates, click on Development which is on the left side bar. 

Select only Apple Push Notification service SSL (Sandbox) and click Next.

Once you are on the next page, select the App ID which we have created in our previous step.

Here, we are required to create CSR File. Go to Keychain Access on your Mac and go to Keychain Access > Certificate Assistant > Request a Certificate From a Certain Authority. 

Save the certificate wherever you are comfortable with and upload it on your next step.

Then Download it onto your computer and click to open it in which it will be inserted onto your Keychain Access.

Then, we gotta create a .p12 file from Keychain Access by right clicking it and Export and when prompted for password, you may just leave it empty. You will notice a Certificates.p12 on your desktop if that is the place where you store it. 

We have to go back to Firebase Console and look for your project. And click on the Setting icon and click Project Settings and go to Cloud Messaging and upload Certificates.p12.

Leave the password blank as we didn’t create any password unless you did created a password. 

With all this configuration, we are almost there. We need to create the entitlement which will be shown on the File Explorer on the left when we turn on Push Notifications.

We are DONE! We are only done with the configuration. We needed to add some code right now.

CODING TIME

Go into AppDelegate.swift and import the following Module

import Firebase
import FirebaseInstanceID
import FirebaseMessaging
import UserNotifications

We will implement UNUserNotificationCenterDelegate & MessagingDelegate protocol.

Here in didFinishLaunchingWithOptions, we checked the device version because not everyone updates their iPhone to the latest version. With the following code inside, we instantiate the protocol implemented.

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        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)
        }
        
        application.registerForRemoteNotifications()
        Messaging.messaging().delegate = self

        FirebaseApp.configure()
        return true
    }

}

You may now send your first Push Notification Message from Cloud Messaging

You might have to try it on your device instead of simulator. Now it should ask for permission to send push notifications. The completed project can be found on GitHub.

  • Article By :
    Founder of DaddyCoding. Studied Computer Science, Information System and Information Technology at BYU-Hawaii. Currently spending most of my time researching and learning on helping to expose making iOS apps.

Random Posts

Leave a Reply

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

*
*