Push Notifications are a robust and effective method for mobile applications to communicate with consumers. You can engage your users by allowing them to perceive values through their own eyes.
By informing your users about something significant, you may engage them.Push notifications on mobile apps may increase app engagement by up to 88 percent, while 65 percent of users revisit an application within 30 days.
Firebase Notifications is one method for implementing push notifications in iOS applications. To get push notifications, your device must be registered with Apple’s Push Notification service (APNs) via the receipt of a unique device token.
After registering the device, you may deliver push notifications to it by making a request to APNs with the device token. All of this communication must take place via a web server. You can hire Flutter developer from Flutter Agency, they do this process seamlessly for your projects.
Firebase Configuration – Creating the p8 Certificate
Firebase mandates that you provide a p8 certificate in your application. This unique file contains the private key required for Firebase to send alerts. Sign in to Apple Developer to obtain a p8 certificate.
- Proceed to Keys after selecting Certificates, Identifiers, and Accounts. To create a new key, click the + button.
- Provide a name for it and activate the Apple Push Notifications service (APNs).
Setting up the Firebase Project
In the upper-right corner of the screen, navigate to your Firebase account and click Go to console. Select Add project and follow the on-screen instructions to create your project:
- Give the project a name.
- Allow Google Analytics to function.
- Give Google Analytics a name and a country.
- Utilize the default analytics configuration.
Then, configure Firebase using your Apple p8 and membership credentials.
Integrating the Package
Now, you’ll add the Firebase component to your project using Swift Package Manager. Select File -> Swift Packages -> Add Package Dependency from inside Xcode. Enter https://github.com/firebase/firebase-ios-sdk.git in the Choose Package Repo pop-up.
Continue selecting Next while maintaining the default parameters until you reach a screen displaying a list of packages. This process may take some time while Xcode downloads the required data. From the list, select the following packages:
- FirebaseAnalytics
- FirebaseMessaging
If you do not wish to gather information about how your users interact with push notifications, uncheck FirebaseAnalytics. After installing these packages, it may take a few moments for the dependencies to be added and built.
Configuration of Your Application
To begin, open Info.plist and insert the following entry:
- Key: FirebaseAppDelegateProxyEnabled
- Value: NO
- Type: Boolean
By default, FirebaseMessaging handles push notifications through the swizzling mechanism. Because you’ll be handling all of the code yourself, disable this using the plist entry you just entered.
Following that, you’ll add an application delegate to your project, who will be in charge of configuring push notifications. Create a new Swift file named AppDelegate.swift and replace the following code with it:
import UIKit import Firebase import FirebaseMessaging import FirebaseAnalytics class AppDelegate: NSObject, UIApplicationDelegate { func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil ) -> Bool { return true } } |
You need to import the relevant Firebase frameworks in the preceding code and develop the UIApplicationDelegate interface.
Then, under AppMain.swift, add a new property to AppMain:
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate |
This notifies SwiftUI about the newly generated app delegate. You may now begin configuring Firebase.
Establishing Firebase
Because you are not allowing Firebase to handle notification code automatically via swizzling, you must comply with UNUserNotificationCenterDelegate. To the end of AppDelegate.swift, add the following:
extension AppDelegate: UNUserNotificationCenterDelegate { func userNotificationCenter( _ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void ) { completionHandler([[.banner, .sound]]) } func userNotificationCenter( _ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void ) { completionHandler() } } |
Subscribing to Notifications
After configuring Firebase, you may begin registering for alerts. Add the following method to the extension UNUserNotificationCenterDelegate:
func application( _ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data ) { Messaging.messaging().apnsToken = deviceToken } |
When a user authorizes authorization for push notifications, APNs generate and register a token. This token uniquely identifies the device, allowing you to send alerts to it. You may now deliver your notifications using Firebase, as this code makes the token accessible in Firebase.
At the bottom of your file, add the following extension:
extension AppDelegate: MessagingDelegate { func messaging( _ messaging: Messaging, didReceiveRegistrationToken fcmToken: String? ) { let tokenDict = [“token”: fcmToken ?? “”] NotificationCenter.default.post( name: Notification.Name(“FCMToken”), object: nil, userInfo: tokenDict) } } |
Messaging is a class in Firebase that controls all aspects of push notifications. As is the case with many iOS APIs, it includes a delegate named MessagingDelegate, which you configure in the preceding code. When your app begins, or Firebase changes your token, Firebase will invoke the method you just created to keep your app up to date.
Final words
We hope this post helped you gain a comprehensive understanding of Firebase Cloud Messaging and how you can quickly take your iOS application to the next level by leveraging push notifications to engage users and increase retention. We suggest you to hire flutter developer so that you may speed up your development cycle.