Issues required to combine Push notification
Earlier than we bounce 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 checklist of issues required to combine and test push notification integration in iOS.
- iPhone gadget – Push notification didn’t work on simulator
- Apple developer account – Apple developer account is required so as to run app on iPhone gadget and establishing Auth key or certificates on firebase app console.
- App on Firebase.(You should utilize your gmail account to create an app on Firebase )
Establishing app
First open up Xcode and create a fundamental challenge(if you’re utilizing an current challenge then open it up). First step is to activate push notification capabilities for our app. Observe beneath steps to activate push notification functionality in your iOS app.
- Choose your challenge title. See image for reference
- Choose your app goal. Choose Signing and capabilities.
- Click on on + Functionality.
- Seek for Push notification and click on on searched consequence exhibiting Push Notification.
Creating app on Firebase
- Open https://firebase.google.com , and click on on Go to console or Check in.
- When you reached your Firebase console. Click on on Add Venture.
- Enter your challenge title. Click on Proceed.
- Analytics is really helpful. Nevertheless it’s your alternative. For this tutorial, i made oi disabled.
- Click on Create Venture. After few seconds Firebase challenge is created and you’ll get message “Your new challenge is prepared,”. Click on on proceed.
- Click on on iOS icon to start out process of including Firebase to your iOS app.
Including Firebase to iOS app
Copy bundle identifier from xcode challenge(Proven in beneath image), and add it to Firebase app bundle if textbox.
You may give nick title to your app on Firebase and may add app retailer id for the app (if added to apple join). Each of those steps are non-compulsory. Subsequent we have to obtain .plist file supplied by Firebase and add that .plist named as “GoogleService-Data.plist” to your challenge. Lastly we have to add Firebase libraries to our xcode challenge. We’ll going to make use of cocoapods for our xcode challenge.
You’ll find checklist of firebase pods from this hyperlink https://firebase.google.com/docs/ios/setup?authuser=0
Putting in Firebase messaging pod
For this tutorial, we’re solely curious about Firebase messaging pod. Since we’re solely integrating push notification with Firebase in IOS.
- Open terminal and kind command
- cd “path root listing of your challenge (the place .xcodeproj file is situated)” Tip:- Merely click on on any file in your xcode challenge 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 current xcode challenge and reopen it with double faucet on file title having extension .xcworkspace
Open AppDelegate.swift and add beneath code
import UIKit import FirebaseCore import FirebaseMessaging @important class AppDelegate: UIResponder, UIApplicationDelegate { func utility(_ utility: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override level for personalisation after utility launch. FirebaseApp.configure() return true }
Above code will initialize our Firebase object.
Requesting consumer permission for Push notification
Subsequent step is to request permissions from consumer for the push notification. Add beneath code to your utility: didFinishLaunchingWithOptions technique.
import UIKit import FirebaseCore import FirebaseMessaging @important class AppDelegate: UIResponder, UIApplicationDelegate { func utility(_ utility: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override level for personalisation after utility launch. FirebaseApp.configure() 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 } } utility.registerForRemoteNotifications() return true }
In above code, we use UserNotificationCentre to request consumer 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 are going to implement delegate that may give us gadget token. If you wish to know fundamental mechanism behind how push notification works in apple ecosystem then please test beneath hyperlink
https://stackoverflow.com/questions/17262511/how-do-ios-push-notifications-work
Getting gadget token
extension AppDelegate: UNUserNotificationCenterDelegate { func utility(_ utility: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Information) { Messaging.messaging().apnsToken = deviceToken } func utility(_ utility: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { print("Did not register with push") } }
Delegate named didRegisterForRemoteNotificationsWithDeviceToken, offers us distinctive token for the gadget which needs to obtain apple push notification. We’ll set this token to Firebase messaging apnsToken. You possibly can ship this apns token to your server in case your server is sending notifications to you. Second delegate, named didFailToRegisterForRemoteNotificationsWithError will will get known as if we fail to get gadget token.
Be aware:- Since Firebase by default use technique swizzling, so we have to flip it off if we need to get push as we’re mapping gadget token in IOS didRegisterForRemoteNotificationsWithDeviceToken delegate and never utilizing Firebase token handler. We are able to disable technique swizzling, by setting a key in data.plist file of our xcode challenge. Test beneath picture.
Including UNUserNotificationCenterDelegate
UNUserNotificationCenterDelegate has two delegates that we have to implement
- willPresent notification :- This delegate will will get known as as soon as we obtain push notification and our app is in foreground.
- didReceive response:- This delegate will will get known as when consumer clicks on notification.
func userNotificationCenter(_ heart: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { print("Will will get known as when app is in forground and we need to present banner") completionHandler([.alert, .sound, .badge]) } func userNotificationCenter(_ heart: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { print("Will will get known as when consumer faucet on notifictaion") completionHandler() } }
Creating Auth key
Open https://developer.apple.com/account/ and choose Certificates, identifiers and profiles choice. On left menu click on on auth key choice. Create it and obtain it to secure 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 staff id yow will discover your Workforce ID within the Apple Member Heart below the membership tab
Including Auth key to Firebase challenge
Lastly we have to add auth key or push certificates to Firebase challenge. Go to your Firebase challenge, and click on on gear icon on high left nook. Choose “Venture 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 staff ID in required fields. Click on add.
Take a look at Push notification
Run your app from xcode to actual iPhone gadget. Permit push notification and ship app to background. Now, open up Firebase challenge and discover “Cloud Messaging” from left menu. Click on on it. If this your first message you will note button having textual content “Ship you first message”. Fill within the required kind .Click on Evaluation, a pop up will present up in your display. Click on Publish. Your gadget will obtain a push notification.
The place to go from right here
On this tutorial, we realized methods to combine push notification in iOS app utilizing Firebase cloud messaging in swift. We coated all steps from begin to finish the place we obtained a push notification from Firebase. If you’re in search of video tutorial for identical then go to beneath hyperlink
https://youtu.be/Tjg5X30XhMw