Push Notification

Overview#

We use APNS services to receive messages in background.

Prerequisites#

Please upload your APNS and VOIP Certificate to our Console. To know more how to generate and upload APNS and VOIP Certificate Click Here

In your Appdelegate, register for remote notification by conforming to the delegates UNUserNotificationCenterDelegate.

Call the below methods when an APNS token gets updated.

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    // APNS Token Update
    VOIPManager.sharedInstance.savePushToken(token: token)
    VOIPManager.sharedInstance.updateDeviceToken()
    }
    ArgumentDescription
    TOKENApns Token

    Capabilities#

    To receive push notification in the background or in the killed state, You need to enable below capabilities.

    Capabilities
    Background Modes

    Now, go to the background mode and enable the below given modes

    Background Modes
    Remote notifications

    App Group Id#

    To receive messages in the background or in the killed state, You need to enable app group container ID.

    In order to access data between app and notification extension, enabling app group is mandatory to access user defaults and database in container.

    Capabilities

    Process push notification request#

    let LICENSE_KEY = "xxxxxxxxxxxxxxxxxxxxxx"
    let CONTAINER_ID = "xxxxxxxxxxxxxxxxxxxxxx"

    We have to set CONTAINER_ID in Appdeleagte class.

    In function didFinishLaunchingWithOptions use this ChatManager.setAppGroupContainerId(id: CONTAINER_ID) method to set CONTAINER_ID which is your App Group string name

      func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
      ChatManager.setAppGroupContainerId(id: CONTAINER_ID)
      ChatManager.initializeSDK(licenseKey: LICENSE_KEY) { isSuccess, error, data in
      }
      }

      Parameter Description#

      FunctionParameter TypeDescription
      CONTAINER_IDStringCreate App groups and assign the obtained group container id. SDK uses this string to create the path for the local storage.
      caution

      Changing existing app group container may cause data loss.

      As a process of getting push notification in background and Killed state, the SDK require to add Notification Extension service in your Xcode project.

      Notification service extension can be acts as a mediator between the User Interface and the APNS. Like all other iOS extensions, such an extension can simply be added to the app in the form of another target.

      Step 1#

      Create NotificationExtension Target

      In Xcode -> File -> New -> Target -> ' NotificationServiceExtension' -> Next -> TargetName -> Activate Scheme

      You can see New target and NotificationService folder been created in your project.

      Step 2#

      Add MirrorflySDK to the new target created for NotificationExtension in Podfile

      target 'MirrorflyNotificationExtention' do
      pod 'MirrorFlySDK', '5.16.3'
      end

      Step 3#

      Do Pod install

      Step 4#

      Start using the SDK using the below statement

      import MirrorFlySDK

      Step 5#

        func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        ChatManager.setAppGroupContainerId(id: CONTAINER_ID)
        ChatManager.initializeSDK(licenseKey: LICENSE_KEY) { isSuccess, error, data in
        }
        NotificationMessageSupport.shared.didReceiveNotificationRequest("NOTIFICATION_CONTENT", "COMPLETION_BLOCK")
        }

        Re-initialize the ChatSDK method and setAppGroupContainerId method inside func didReceive method of the UNNotificationServiceExtension delegate.

        In class that extends the UNNotificationServiceExtension call the above NotificationMessageSupport.shared.didReceiveNotificationRequest method to process the request further.

        ArgumentTypeDescription
        NOTIFICATION_CONTENTUNMutableNotificationContentNotification request content
        COMPLETION_BLOCK(UNMutableNotificationContent?, ChatMessage?) -> VoidCompletion Block