Skip to main content

Push Notifications

Firebase configuration#

In order to enable MeetingLawyers Chat notifications, there are two posibilities:

1.- If you have your own Firebase app declared, you must provide us google-services.json from your Firebase workspace.

2.- If you don't have a Firebase and don't want to create it, we can provide one. For Android we need your App Package, and for iOS we need .p8 Certificate file, Team ID, and the certificate key. Once Firebase app are created, we'll provide you google-services.json and GoogleService-info.plist files to add to your apps.

See: Firebase Documentation

Sample: Our example with push notification

Push notifications#

MeetingLawyersSDK framework uses push notifications to communicate users pending messages, these notifications are served by Firebase SDK.

As soon as the on-screen chat presentation starts, if permissions to send notifications have not yet been needed by the host application, the system security dialog is displayed. When the user grants the necessary permissions to send push notifications, it is mandatory to intervene system remote notification calls to notify MeetingLawyersSDK with the new device token. To do so, we require to implement the following methods in your AppDelegate.swift:

AppDelegate.swift
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
// Create a class to delegate all push configuration
private let pushNotificationSample = PushNotificationSample()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
pushNotificationSample.configurePush(application: application)
return true
}
}

Now create PushNotificationSample class to unify all code and make more clean our code:

PushNotificationSample.swift
import FirebaseMessaging
import MeetingLawyers
import UIKit
import FirebaseCore
import Foundation
class PushNotificationSample: NSObject {
public func configurePush(application: UIApplication) {
// Enable firebase push notification
registerFirebasePushNotification()
// Enable Push notification
registerForPushNotifications(application: application)
}
}
PushNotificationSample.swift
extension PushNotificationSample {
internal func registerFirebasePushNotification() {
FirebaseApp.configure()
Messaging.messaging().delegate = self
}
internal func registerForPushNotifications(application: UIApplication) {
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: { _, _ in }
)
application.registerForRemoteNotifications()
}
}

Receive push token:

PushNotificationSample.swift
// MARK: Firebase Implementation
extension PushNotificationSample: MessagingDelegate {
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
if let fcmToken = fcmToken {
Task {
print("[ApplicationDelegate] Firebase registration token: \(fcmToken)")
do {
try await MeetingLawyersApp.setFirebaseMessagingToken(token: fcmToken)
print("[ApplicationDelegate] Token registered correctly")
} catch {
print("[ApplicationDelegate] Error registering token: \(error)")
}
}
}
}
}

Now, the app can receive chat notifications. After that you must pass notifications to library methods to process the Push Notification info:

PushNotificationSample.swift
// MARK: Push notification Implementation
extension PushNotificationSample: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ userNotificationCenter: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
_ = MeetingLawyersApp.userNotificationCenter(willPresent: notification.request, completionHandler: completionHandler)
}
func userNotificationCenter(_ userNotificationCenter: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
_ = MeetingLawyersApp.userNotificationCenter(didReceive: response.notification.request) { error in
print("ERROR \(error.debugDescription)")
completionHandler()
}
}
}

Handle didReceive error#

If there is any error using func userNotificationCenter(_ userNotificationCenter: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) should be handled and the result redirected.

Top ViewController

The SDK can handle the notification only if the top view controller is an instance of ProfessionalListViewController, MessagesViewController or ProfileViewController. If the top view controller is not from the SDK, for example it is one from your application, the method will return an MeetingLawyersError error.

The error can have one of these values:

  • actionFailed(reason: ActionFailureReason)
  • pushesFailed(reason: PushesFailureReason)

And if the value is actionFailed case, the reason values of this error are one of the following they can be used in the execute function:

  • executionError(pendingAction: MeetingLawyersAction)

The variable of type MeetingLawyersAction should be saved until an instance of the SDK is rendered as Top ViewController. At this moment you can call the method func execute(action: MeetingLawyersAction, origin: UIViewController?, completionHandler: @escaping (MeetingLawyersError?) -> Void) to continue with the navigation.

// Handle error
if case let .actionFailed(reason) = error,
case let .executionError(action) = reason {
// Save action to execute later
}
completionHandler()
note

If you use Firebase notifications with Certificates instead Key Authorization (.p8), you must add the next piece of code:

AppDelegate.swift
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
_ = MeetingLawyersApp.userNotificationCenter(didReceive: response.notification.request) { error in
print("ERROR \(error.debugDescription)")
completionHandler()
}
}
caution
  • It is necessary that the host application has the necessary permissions and entitlements to receive push notifications.

  • It is essential to provide an Authorization key to the administrator of your MeetingLawyer account so that notifications are received correctly.

  • It is highly recommended to implement background fetch result and modify your app capabilities to include 'fetch' and 'remote-notification' entitlements.

<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>remote-notification</string>
</array>

Next steps#