Friday, April 12, 2024
HomeIOS DevelopmentCombine push notification utilizing Firebase messaging in swift – iOS swift tutorial...

Combine push notification utilizing Firebase messaging in swift – iOS swift tutorial – iOSTutorialJunction

Issues required to combine Push notification

Earlier than we soar on to combine push notification utilizing Firebase messaging in iOS swift, you want few issues at your disposal to check iOS push notification. Beneath is the record of issues required to combine and examine push notification integration in iOS.

  • iPhone system – Push notification didn’t work on simulator
  • Apple developer account – Apple developer account is required with the intention to run app on iPhone system and establishing Auth key or certificates on firebase app console.
  • App on Firebase.(You need to use your gmail account to create an app on Firebase )

Establishing app

First open up Xcode and create a primary mission(in case you are utilizing an present mission then open it up). First step is to activate push notification capabilities for our app. Comply with beneath steps to activate push notification functionality in your iOS app.

Steps to activate push functionality
  1. Choose your mission identify. See image for reference
  2. Choose your app goal. Choose Signing and capabilities.
  3. Click on on + Functionality.
  4. Seek for Push notification and click on on searched end result displaying Push Notification.

Creating app on Firebase

  • Open , and click on on Go to console or Register.
  • When you reached your Firebase console. Click on on Add Undertaking.
  • Enter your mission identify. Click on Proceed.
  • Analytics is really helpful. But it surely’s your alternative. For this tutorial, i made oi disabled.
  • Click on Create Undertaking. After few seconds Firebase mission is created and you’ll get message “Your new mission is prepared,”. Click on on proceed.
  • Click on on iOS icon to begin process of including Firebase to your iOS app.

Including Firebase to iOS app

Copy bundle identifier from xcode mission(Proven in beneath image), and add it to Firebase app bundle if textbox.

The place to seek out bundle id in xcode mission

You can provide nick identify to your app on Firebase and might add app retailer id for the app (if added to apple join). Each of those steps are non-obligatory. Subsequent we have to obtain .plist file offered by Firebase and add that .plist named as “GoogleService-Data.plist” to your mission. Lastly we have to add Firebase libraries to our xcode mission. We are going to going to make use of cocoapods for our xcode mission.

You could find record of firebase pods from this hyperlink

Putting in Firebase messaging pod

For this tutorial, we’re solely all for Firebase messaging pod. Since we’re solely integrating push notification with Firebase in IOS.

  • Open terminal and sort command
  • cd “path root listing of your mission (the place .xcodeproj file is positioned)” Tip:- Merely click on on any file in your xcode mission and click on present in finder
  • pod init
  • open -e podfile
  • As soon as podfile is opened in TextEdit paste beneath line to it, Save file and go t o terminal window
  • pod set up
  • After profitable set up shut your present xcode mission and reopen it with double faucet on file identify having extension .xcworkspace

Open AppDelegate.swift and add beneath code

import UIKit
import FirebaseCore
import FirebaseMessaging

class AppDelegate: UIResponder, UIApplicationDelegate {

    func software(_ software: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override level for personalization after software launch.
        return true

Above code will initialize our Firebase object.

Requesting person permission for Push notification

Subsequent step is to request permissions from person for the push notification. Add beneath code to your software: didFinishLaunchingWithOptions methodology.

import UIKit
import FirebaseCore
import FirebaseMessaging

class AppDelegate: UIResponder, UIApplicationDelegate {

    func software(_ software: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override level for personalization after software launch.
        UNUserNotificationCenter.present().delegate = self
        let authOptions: UNAuthorizationOptions = [.alert, .sound,.badge]
        UNUserNotificationCenter.present().requestAuthorization(choices: authOptions) { success, error in
            if error != nil {
                //we're able to go
        return true

In above code, we use UserNotificationCentre to request person to permit push notification having permissions for alert, sound and badge.

Conforming to UNUserNotificationCenterDelegate protocol

We have to implement delegates required for Push notification. First we’ll implement delegate that can give us system token. If you wish to know primary mechanism behind how push notification works in apple ecosystem then please examine beneath hyperlink

Getting system token

extension AppDelegate: UNUserNotificationCenterDelegate {
    func software(_ software: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Knowledge) {
        Messaging.messaging().apnsToken = deviceToken
  	func software(_ software: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("Did not register with push")

Delegate named didRegisterForRemoteNotificationsWithDeviceToken, provides us distinctive token for the system which needs to obtain apple push notification. We are going to set this token to Firebase messaging apnsToken. You may ship this apns token to your server in case your server is sending notifications to you. Second delegate, named didFailToRegisterForRemoteNotificationsWithError will will get referred to as if we fail to get system token.

Observe:- Since Firebase by default use methodology swizzling, so we have to flip it off if we wish to get push as we’re mapping system token in IOS didRegisterForRemoteNotificationsWithDeviceToken delegate and never utilizing Firebase token handler. We will disable methodology swizzling, by setting a key in information.plist file of our xcode mission. Verify beneath picture.

Turning off Firebase methodology swizzling

Including UNUserNotificationCenterDelegate

UNUserNotificationCenterDelegate has two delegates that we have to implement

  • willPresent notification :- This delegate will will get referred to as as soon as we obtain push notification and our app is in foreground.
  • didReceive response:- This delegate will will get referred to as when person clicks on notification.
    func userNotificationCenter(_ heart: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        print("Will will get referred to as when app is in forground and we wish to present banner")

        completionHandler([.alert, .sound, .badge])
    func userNotificationCenter(_ heart: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        print("Will will get referred to as when person faucet on notifictaion")

Creating Auth key

Open and choose Certificates, identifiers and profiles choice. On left menu click on on auth key choice. Create it and obtain it to protected place as we’d like this key file in subsequent step, all steps are self explanatory. As soon as your authkey is generated by apple, copy “Key ID”. Lastly, we required group id yow will discover your Workforce ID within the Apple Member Middle below the membership tab

Including Auth key to Firebase mission

Lastly we have to add auth key or push certificates to Firebase mission. Go to your Firebase mission, and click on on gear icon on high left nook. Choose “Undertaking Settings” -> “Cloud Messaging” -> Scroll all the way down to IOS app and click on on “Add” button below “APNs Authentication Key” part. Add auth key file created in final part, and key ID with group ID in required fields. Click on add.

Check Push notification

Run your app from xcode to actual iPhone system. Enable push notification and ship app to background. Now, open up Firebase mission and discover “Cloud Messaging” from left menu. Click on on it. If this your first message you will notice button having textual content “Ship you first message”. Fill within the required kind .Click on Assessment, a pop up will present up in your display. Click on Publish. Your system will obtain a push notification.

The place to go from right here

On this tutorial, we discovered learn how to combine push notification in iOS app utilizing Firebase cloud messaging in swift. We lined all steps from begin to finish the place we acquired a push notification from Firebase. If you’re on the lookout for video tutorial for identical then go to beneath hyperlink



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments