Firebase Push notification | Swift tutorial

Aman Gupta
Aug 18, 2018 · 5 min read

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.
Image for post
Image for post
  • 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).
Image for post
Image for post
  • Select Push notification.
  • Continue and register the App ID.
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

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.
Image for post
Image for post
  • Now select the “Apple Push Notification service SSL (Sandbox)”.
Image for post
Image for post
  • Continue and select your APP ID.
Image for post
Image for post
  • Continue and Select the Your Computer CSR File.
Image for post
Image for post
  • For Create the CSR Open the keyChain in your computer.
  • select Keychain Access > Certificate Assistant > Request a Certificate from a Certificate Authority
Image for post
Image for post
  • 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.
Image for post
Image for post
  • 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.
Image for post
Image for post
  • By Double click on Downloaded certificate install the certificate
  • Now Right click on Install certificate in keychain and Export the .p12 file.
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

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.
Image for post
Image for post
  • Create the new project According to your project name
Image for post
Image for post
  • Open the project setting
Image for post
Image for post
  • Select “Add firebase to your IOS app”
Image for post
Image for post
  • Add your bundle identifier name and Project nick name(optional)
Image for post
Image for post
  • Download the plist and drag and drop into your project
Image for post
Image for post
  • Install the pod into your project
Image for post
Image for post
pod 'Firebase/Core
Firebase/Messaging
  • Add billow code into the app delegate “didFinishLaunchingWithOptions” function
Image for post
Image for post
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.

Image for post
Image for post
  • 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”
Image for post
Image for post
  • And last Go to Target-> User Segment -> Select your IOS App, After that send the message.
Image for post
Image for post

You Can Follow This Complete tutorial on

developerfly.com

Originally published at developerfly.com on March 15, 2018.

developerfly

Developers content

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store