Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // PushNotificationHandler.swift
- // UltimateMyXL
- //
- // Created by Armadiaz on 16/09/20.
- // Copyright © 2020 Aleph. All rights reserved.
- //
- import Firebase
- import UIKit
- import XLAlbums
- class PushNotificationHandler: NSObject, MessagingDelegate, UNUserNotificationCenterDelegate {
- static let shared = PushNotificationHandler()
- override init() {
- super.init()
- }
- // REGISTER PUSH NOTIFICATION
- func registerForPushNotifications() {
- if #available(iOS 10.0, *) {
- // For iOS 10 display notification (sent via APNS)
- UNUserNotificationCenter.current().delegate = self
- let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
- UNUserNotificationCenter.current().requestAuthorization(
- options: authOptions,
- completionHandler: { _, _ in })
- // For iOS 10 data message (sent via FCM)
- Messaging.messaging().delegate = self
- } else {
- let settings: UIUserNotificationSettings =
- UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
- UIApplication.shared.registerUserNotificationSettings(settings)
- }
- UIApplication.shared.registerForRemoteNotifications()
- }
- // RECEIVE TOKEN
- func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
- Dispatch.main {
- // Send notification token to middleware
- Preference.set(value: fcmToken, forKey: .kPushNotificationToken)
- if CoreDataUtils.fetchAllUsers().count > 0 {
- SessionRepo().sendNotificationTokenToMW(token: fcmToken) { (response, error) in
- if let res = response {
- dprint(res)
- } else if let err = error {
- dprint(err)
- }
- }
- }
- }
- }
- // SHOW NOTIFICATION BANNER WHEN APP IS IN FOREGROUND
- func userNotificationCenter(
- _ center: UNUserNotificationCenter,
- willPresent notification: UNNotification,
- withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
- let userInfo = notification.request.content.userInfo
- print(userInfo)
- // PRESENT NOTIFICATION BANNER
- completionHandler([[.alert, .sound]])
- }
- // TRIGGERED WHEN USER PRESS THE NOTIFICATION BANNER
- func userNotificationCenter(
- _ center: UNUserNotificationCenter,
- didReceive response: UNNotificationResponse,
- withCompletionHandler completionHandler: @escaping () -> Void) {
- // return if there's no user
- Dispatch.main {
- guard CoreDataUtils.fetchAllUsers().count > 0 else {
- completionHandler()
- return
- }
- // Get notification "data" payload sent from FCM
- var userInfo = response.notification.request.content.userInfo
- if let moengageInfo = userInfo["app_extra"] as? [AnyHashable: Any], let screenInfo = moengageInfo["screenData"] as? [AnyHashable: Any] {
- userInfo = screenInfo
- }
- var notificationPayload: NotificationPayloadModel
- // Converting notification payload data (Dictionary) as NotificationPayloadModel (Struct)
- if let formId = userInfo["surveyId"] as? String, !formId.isEmpty {
- notificationPayload = NotificationPayloadModel(actionType: "SURVEY", actionParam: formId)
- } else {
- notificationPayload = NotificationPayloadModel(dictionary: userInfo)
- }
- // check for correct user if it exist based on user subs ID
- if let notificationSubID = notificationPayload.subscriptionId, !notificationSubID.isEmpty, let targetUserBySubsID = CoreDataUtils.fetchUser(with: notificationSubID) {
- // Try to switch user based on User SubscriberID
- if targetUserBySubsID.subscriberID != CoreDataUtils.getActiveUser()?.subscriberID {
- CoreDataUtils.setUserAsActive(user: targetUserBySubsID)
- Preference.set(value: targetUserBySubsID.isCorporate, forKey: .kIsEnterprise)
- }
- // check for correct user if it exist based on user MSISDN
- } else if let notificationMSISDN = notificationPayload.msisdn, !notificationMSISDN.isEmpty, let targetUserByMSISDN = CoreDataUtils.fetchUserWithMSISDN(msisdn: notificationPayload.msisdn ?? "") {
- // Try to switch user based on User MSISDN
- if targetUserByMSISDN.profile?.msisdn != CoreDataUtils.getActiveUser()?.profile?.msisdn {
- CoreDataUtils.setUserAsActive(user: targetUserByMSISDN)
- Preference.set(value: targetUserByMSISDN.isCorporate, forKey: .kIsEnterprise)
- }
- }
- self._processDeeplink(notificationPayload: notificationPayload)
- completionHandler()
- }
- }
- func _processDeeplink(notificationPayload: NotificationPayloadModel) {
- let dynamicLinkData = DynamicLinkModel(param: [notificationPayload.actionType ?? "": notificationPayload.actionParam ?? ""])
- Preference.setStruct(dynamicLinkData, forKey: .kDynamicLinkData)
- self._processPushNotification()
- }
- private func _processPushNotification() {
- if let tabBarController = UIApplication.shared.windows.first?.rootViewController as? XLTabBarController {
- // Handle the notification when the app is in foreground but not on Dashboard
- if tabBarController.selectedIndex != 0 {
- AppDelegate.shared.setTabbarAsRoot(transition: .init(direction: .fade))
- } else if let navController = tabBarController.viewControllers?.first as? XLNavigationController {
- if let viewController = navController.viewControllers.first as? DashboardViewController {
- viewController.presenter.handleDynamicLink()
- } else if let viewController = navController.viewControllers.first as? DashboardHomePostpaidViewController {
- viewController.presenter.handleDynamicLink()
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment