Firebase Push notification | Swift tutorial

Push Notification by firebase in IOS.For Firebase Push notification we need to have 

1
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 
      1
      Bundle Identifier

       (Bundle ID) of your App (Please verify that your bundle identifier is unique).Form For create new App IDs

    • Select 
      1
      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 
      1
      CSR

       generating process.

    • Add CSR file and continue.
  • Now again to your APP ID and edit them and Download your 
    1
    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

1
2
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

1
        <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

1
2
<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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<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>
       
        completionHandler([])
    }
   
    <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)
        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.

 

Related Post

Compile C/C++ program (Dev-C/C++) what is Dev-C/C++? Dev-C/C++, developed by Bloodshed Software, is a fully featured graphical IDE(Integrated Development Environment),which ...
New Tools for 64-bit Support in Xcode 9.3   New Tools for 64-bit Support in Xcode 9.3 The last macOS release to support 32-bit apps without compromise is macOS High Sierra. Make sure fu...
E-Mail Format Formal Email / Letter Format A formal email is in general sent to someone you don’t know well or to someone who’s in authority. Examples of someone w...
Introduction to JVM Introduction to JVM JVM stands for Java Virtual Machine. It works as an engine that derives the Java Code. The JVM does not understand Java ty...
C Language – Introduction Why C? There are three important aspects of any language so as C has : The way it stores data, The way it operates on this data and How th...
Create Gradient layer by the use of CAGradientLaye... Create Gradient layer by the use of CAGradientLayer Mostly developers are use the gradient (Combination of colors) colors images for make app more ef...

Leave a Reply

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