Firebase Push notification | Swift tutorial

Push Notification by firebase in IOS.For Firebase Push notification we need to have paid apple developer account.
You need to Enroll into the Apple Developer Program ($99 per year) to unlock the push notifications.
Now we are consider you have this account for push notification. lets start the tutorial
For firebase push notification we need to follow different steps

  • Setup (Configure) apple developer account
  • create APNS certificate for uploading into firebase account OR
  • Create APNS authentication key for upload into firebase account
  • setup (Configure) firebase push notification




Configure apple developer account

  • First SignIn On Apple developer account and and tap on Side Bar button or center button “Certificate, IDs & Profile.
  • Now select tab from the side menu identifiers/App IDs. After clicking on this select the add button which are present at right corner for creating/Registering the IOS App ID.
    Create App ID
  • Register your app ID
    • Enter your app id description/Name.
    • Enter the Bundle Identifier (Bundle ID) of your App (Please verify that your bundle identifier is unique).Form For create new App IDs
    • Select Push notification.
    • Continue and register the App ID. 


Create APNS certificate

  • Now you want to Create the “Apple Push Notification service SSL (Sandbox)” certificate
    • GO to Certificate tab add new certificate by tap + Icon.
    • Now select the “Apple Push Notification service SSL (Sandbox)”.
    • Continue and select your APP ID.
    • Continue and Select the Your Computer CSR File.

      • For Create the CSR Open the keyChain in your computer.
      • select Keychain Access > Certificate Assistant > Request a Certificate from a Certificate Authority
      • In the Certificate Information window, enter the following information:
      • In the User Email Address field, enter your email address.
      • In the Common Name field, create a name for your private key (e.g., John Doe Dev Key).
      • The CA Email Address field should be left empty.
      • In the “Request is” group, select the “Saved to disk” option.
    • Click Continue within Keychain Access to complete the CSR generating process.
    • Add CSR file and continue.
  • Now again to your APP ID and edit them and Download your SSL Certificate For development and production Both.
  • By Double click on Downloaded certificate install the certificate
    • Now Right click on Install certificate in keychain and Export the .p12 file.





Setup(Configure) firebase push notification

For Setup the firebase push notification we need to have a firebase account. You can register from here

  • Go to the Firebase Console screen.
  • Create the new project According to your project name
  • Open the project setting
  • Select “Add firebase to your IOS app”
  • Add your bundle identifier name and Project nick name(optional)
  • Download the plist and drag and drop into your project
  • Install the pod into your project
pod 'Firebase/Core
Firebase/Messaging
  • Add billow code into the app delegate “didFinishLaunchingWithOptions” function
        FirebaseApp.configure()
  • Select the cloud messing and upoad the APNS .p12 file for development and production.

Setup the Code in your project

Note:- Must be enable the Push notification into your project. Select the project target and Tab the Capabilities tab and after that enable the push notification services.

  • Import Both frameworks in AppDelegate
      import UserNotifications
      import FirebaseMessaging
  • Add UNUserNotificationCenterDelegate Delegate Methods for receiving the notification into the app.
//MARK: - UIApplicationDelegate Methods
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        let userInfo = notification.request.content.userInfo
        
        
        // Print message ID.
        //    if let messageID = userInfo[gcmMessageIDKey]
        //    {
        //      print("Message ID: \(messageID)")
        //    }
        
        // Print full message.
        print(userInfo)
        
        //    let code = String.getString(message: userInfo["code"])
        guard let aps = userInfo["aps"] as? Dictionary<String, Any> else { return }
        guard let alert = aps["alert"] as? String else { return }
        //    guard let body = alert["body"] as? String else { return }
        
        completionHandler([])
    }
    
    // Handle notification messages after display notification is tapped by the user.
    
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo
        
        print(userInfo)
        completionHandler()
    }
  • For test the push notification go to firebase Notification tab send select the “SEND YOUR FIRST MESSAGE”
  • And last Go to Target-> User Segment -> Select your IOS App,  After that send the message.



    Download the complete project from here.

 

Leave a Reply

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

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.