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.
      • 1
        <code>select Keychain Access &gt; Certificate Assistant &gt; 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 

       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 '<span class="hljs-type">Firebase</span>/<span class="hljs-type">Core</span>
<span class="hljs-type">Firebase</span>/<span class="hljs-type">Messaging</span>
  • Add billow code into the app delegate “didFinishLaunchingWithOptions” function

        <span class="hljs-type">FirebaseApp</span>.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

<span class="hljs-keyword">      import</span> UserNotifications
<span class="hljs-keyword">      import</span> FirebaseMessaging
  • Add UNUserNotificationCenterDelegate Delegate Methods for receiving the notification into the app.

<span class="hljs-comment">//MARK: - UIApplicationDelegate Methods</span>
    <span class="hljs-preprocessor">@available</span>(iOS <span class="hljs-number">10.0</span>, *)
    <span class="hljs-func"><span class="hljs-keyword">func</span> <span class="hljs-title">userNotificationCenter</span><span class="hljs-params">(<span class="hljs-number">_</span> center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions)</span></span> -&gt; <span class="hljs-type">Void</span>) {
        <span class="hljs-keyword">let</span> userInfo = notification.request.content.userInfo
        <span class="hljs-comment">// Print message ID.</span>
        <span class="hljs-comment">//    if let messageID = userInfo[gcmMessageIDKey]</span>
        <span class="hljs-comment">//    {</span>
        <span class="hljs-comment">//      print("Message ID: \(messageID)")</span>
        <span class="hljs-comment">//    }</span>
        <span class="hljs-comment">// Print full message.</span>
        <span class="hljs-built_in">print</span>(userInfo)
        <span class="hljs-comment">//    let code = String.getString(message: userInfo["code"])</span>
        <span class="hljs-keyword">guard</span> <span class="hljs-keyword">let</span> aps = userInfo[<span class="hljs-string">"aps"</span>] <span class="hljs-keyword">as</span>? <span class="hljs-type">Dictionary</span>&lt;<span class="hljs-type">String</span>, <span class="hljs-type">Any</span>&gt; <span class="hljs-keyword">else</span> { <span class="hljs-keyword">return</span> }
        <span class="hljs-keyword">guard</span> <span class="hljs-keyword">let</span> alert = aps[<span class="hljs-string">"alert"</span>] <span class="hljs-keyword">as</span>? <span class="hljs-type">String</span> <span class="hljs-keyword">else</span> { <span class="hljs-keyword">return</span> }
        <span class="hljs-comment">//    guard let body = alert["body"] as? String else { return }</span>
    <span class="hljs-comment">// Handle notification messages after display notification is tapped by the user.</span>
    <span class="hljs-preprocessor">@available</span>(iOS <span class="hljs-number">10.0</span>, *)
    <span class="hljs-func"><span class="hljs-keyword">func</span> <span class="hljs-title">userNotificationCenter</span><span class="hljs-params">(<span class="hljs-number">_</span> center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping ()</span></span> -&gt; <span class="hljs-type">Void</span>) {
        <span class="hljs-keyword">let</span> userInfo = response.notification.request.content.userInfo
        <span class="hljs-built_in">print</span>(userInfo)
  • 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.


Related Post

Variables Variables A variable is a named location in memory that is used to hold a value that can be modified by the program. All variable must be declared be...
Break Statement in JAVA Break Statement in JAVA The break statement is usually used in following two scenarios: The break statement terminates the loop immediately, and...
Introduction To C A Brief History Of C   C was invented and first implemented by Dennis Ritchie on a DEC PDP-11 that used the Unix operating system. C is the r...
Java If-else Statement Java If-else Statement The Java if statement is used to test the condition. It checks boolean condition: true or false. For example, if a number i...
Loops in java Loops in JAVA Loops are used to execute a set of statements repeatedly until a particular condition is satisfied. Loop is used in programming ...
Java Operators Java Operators An operator is used to perform an operation over one or more operands. It is a character that represents an action, for example * is a...

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.