Skip to main content

Push Notifications

Firebase configuration

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

  1. If you have your own Firebase app declared, you must provide us Sender ID and Server Key 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.

Push notifications

MeetingDoctors 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 MeetingDoctors with the new device token. To do so, we require to implement the following methods in your AppDelegate:

caution

The method must be invoked after call MeetingDoctors.authenticate() method

extension ApplicationDelegate: MessagingDelegate {
// Refresh_token
public func messaging(_: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("[ApplicationDelegate] Firebase registration token: \(fcmToken)")
// Note: This callback is fired at each app startup and whenever a new token is generated.
MeetingDoctors.registerFirebaseForNotifications(token: fcmToken) { result in
result.process(doSuccess: { _ in
print("[ApplicationDelegate] Token registered correctly")
}, doFailure: { error in
print("[ApplicationDelegate] Error registering token: \(error)")
})
}
}
}

If the user is not authenticated, the previous function won't succeed so you will have to implement the same function in the succeed's callback of the authenticate function and it will register firebase token:

MeetingDoctors.authenticate(token: userToken) { [weak self] (result: MeetingDoctorsResult<Void>) in

let success = result.isSuccess

if result.isSuccess {
self?.isAuthenticated = success

Messaging.messaging().token { token, error in
if let token = token {
MeetingDoctors.registerFirebaseForNotifications(token: token) { result in
result.process(doSuccess: { _ in
NSLog("[FirebaseApplicationPlugin] Token registered correctly")
completion?(success)
}, doFailure: { error in
NSLog("[FirebaseApplicationPlugin] Error registering token: \(error)")
completion?(success)
})
}
} else if let error = error {
NSLog("[FirebaseApplicationPlugin] Error getting token from firebase: \(error)")
completion?(success)
}
}
}
...

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

func userNotificationCenter(_ userNotificationCenter: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
MeetingDoctors.userNotificationCenter(userNotificationCenter, willPresent: notification) { result in
NSLog("[MeetingDoctorsApplicationPlugin] Case 2")
do {
completionHandler(try result.unwrap())
} catch {
completionHandler([.alert, .badge, .sound])
}
}
}

func userNotificationCenter(_ userNotificationCenter: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
MeetingDoctors.userNotificationCenter(userNotificationCenter, didReceive: response) { result in
NSLog("[MeetingDoctorsApplicationPlugin] Case 3")
result.process(doSuccess: { _ in
completionHandler()
}, doFailure: { error in
if let launchScreen: LaunchScreenViewControllerOld = UIApplication.shared.keyWindow?.rootViewController as? LaunchScreenViewController {
let deeplink = MeetingDoctorsDeeplinkOption.from(userInfo: response.notification.request.content.userInfo)
_ = launchScreen.deeplink(deeplink, animated: false)
completionHandler()
} else {
completionHandler()
}
})
}
}

If you have Firebase notifications by certificates instead Key Authorization (.p8), you must add the next piece of code:

func application(_ application: UIApplication, 
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
MeetingDoctors.didReceiveRemoteNotification(application, with: userInfo) { (result: MeetingDoctorsResult<UIBackgroundFetchResult>) in
do {
completionHandler(try result.unwrap())
} catch {
completionHandler(.failed)
}
}
}
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 MeetingDoctors 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>

Get Pending Messages Count

Once the authentication process is over, we can then request the pending messages to be read by the user using the following method:

func unreadMessageCount(with filter: MeetingDoctorsFilterType, _ completion: @escaping (MeetingDoctorsResult<Int>) -> Void) {

So, once we get the result, we can update application badge icon:

let filter = MeetingDoctorsFilter.default // returns unread messages from all categories
MeetingDoctors.unreadMessageCount(with: filter) { [weak self] result in
let badge = result.value ?? 0
UIApplication.shared.applicationIconBadgeNumber = badge
}