Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Earlier than we soar 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 record of issues required to combine and test push notification integration in iOS.
First open up Xcode and create a fundamental venture(in case you are utilizing an current venture 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.
Copy bundle identifier from xcode venture(Proven in beneath image), and add it to Firebase app bundle if textbox.
You may give nick identify to your app on Firebase and might add app retailer id for the app (if added to apple join). Each of those steps are elective. Subsequent we have to obtain .plist file supplied by Firebase and add that .plist named as “GoogleService-Information.plist” to your venture. Lastly we have to add Firebase libraries to our xcode venture. We’ll going to make use of cocoapods for our xcode venture.
You will discover record of firebase pods from this hyperlink https://firebase.google.com/docs/ios/setup?authuser=0
For this tutorial, we’re solely involved in Firebase messaging pod. Since we’re solely integrating push notification with Firebase in IOS.
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.
Subsequent step is to request permissions from person 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 person to permit push notification having permissions for alert, sound and badge.
We have to implement delegates required for Push notification. First we are going to implement delegate that can give us machine 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 machine token
extension AppDelegate: UNUserNotificationCenterDelegate { func utility(_ utility: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Information) { Messaging.messaging().apnsToken = deviceToken } func utility(_ utility: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { print("Didn't register with push") } }
Delegate named didRegisterForRemoteNotificationsWithDeviceToken, provides us distinctive token for the machine which desires to obtain apple push notification. We’ll set this token to Firebase messaging apnsToken. You may 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 machine token.
Notice:- Since Firebase by default use technique swizzling, so we have to flip it off if we need to get push as we’re mapping machine token in IOS didRegisterForRemoteNotificationsWithDeviceToken delegate and never utilizing Firebase token handler. We will disable technique swizzling, by setting a key in data.plist file of our xcode venture. Verify beneath picture.
UNUserNotificationCenterDelegate has two delegates that we have to implement
func userNotificationCenter(_ middle: 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(_ middle: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { print("Will will get known as when person faucet on notifictaion") completionHandler() } }
Open https://developer.apple.com/account/ and choose Certificates, identifiers and profiles possibility. On left menu click on on auth key possibility. Create it and obtain it to protected 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 group id you could find your Crew ID within the Apple Member Middle underneath the membership tab
Lastly we have to add auth key or push certificates to Firebase venture. Go to your Firebase venture, and click on on gear icon on high left nook. Choose “Venture Settings” -> “Cloud Messaging” -> Scroll right down to IOS app and click on on “Add” button underneath “APNs Authentication Key” part. Add auth key file created in final part, and key ID with group ID in required fields. Click on add.
Run your app from xcode to actual iPhone machine. Permit push notification and ship app to background. Now, open up Firebase venture 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 type .Click on Overview, a pop up will present up in your display. Click on Publish. Your machine will obtain a push notification.
On this tutorial, we discovered combine push notification in iOS app utilizing Firebase cloud messaging in swift. We lined all steps from begin to finish the place we obtained a push notification from Firebase. In case you are in search of video tutorial for identical then go to beneath hyperlink
https://youtu.be/Tjg5X30XhMw