In an automated integration, the AppDelegate replacement method sends method calls to your application’s real AppDelegate. If you do not want to rely on the AppDelegate replacement method, you can perform a manual integration.


Attention:

  • IBM recommends that you do not perform the manual integration unless absolutely necessary. The automated integration method of replacing AppDelegate is recommended for ease of integration and upgrading. Some integration issues might occur if you use the manual integration method.
  • When integrating manually, remember to remove the appDelegateClass section of your MceConfig.json file.

In a manual integration, you add code to the AppDelegate.m file in your app instead of the MCEAppDelegate.m file in the SDK. Additionally, perform the following steps:

  • Set Other Linker Flags to -ObjC.
  • Copy the IBM Mobile App Messaging SDK library into the project.
  • Add the required library files:
    • CoreLocation.framwork
    • MapKit.frameworks
    • libsqlite3.0.tbd
    • CoreData.framework
    • CoreTelephony.framework
  • Copy MceConfig.json to your project. Modify MceConfig.json to include your app keys. Remove the appDelegateClass section.
  • Add the Push Notifications Capability to the target settings. For instructions, see Enabling push notifications in iOS 10 (step 1 in step 1).
  • Attach images, videos, and sound by following the instructions for adding the notification service. For instructions, see Adding media attachments to push notifications in iOS 10.
  • To enable Swift to use the classes in the SDK, you must use a bridging header in your application and include the following line in the header:
    #import <IBMMobilePush/IBMMobilePush.h>

    For more information, see http://apple.co/1NeqEPF. If you are creating a bridging header for the first time, ensure that you specify the bridging header filename in Build Settings > Swift Compiler > Code Generation > Objective-C Bridging Header.

To manually integrate the iOS SDK, integrate the following code into your app delegate file:

import UIKit

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        MCESdk.shared.handleApplicationLaunch()

        if #available(iOS 10.0, *) {
            application.registerForRemoteNotifications()
            let center = UNUserNotificationCenter.current()
            center.delegate=MCENotificationDelegate.shared
                center.requestAuthorization(options: [.alert, .sound, .carPlay, .badge], completionHandler: { (granted, error) in
print("Notifiations response (granted) (error)")
DispatchQueue.main.sync
{ application.registerForRemoteNotifications() }

})
        else if #available(iOS 8.0, *) {
            let settings = UIUserNotificationSettings(types: [.badge, .sound, .alert], categories:nil)
            application.registerUserNotificationSettings(settings)
            application.registerForRemoteNotifications()
        } else {
            application.registerForRemoteNotifications(matching: [.badge, .alert, .sound])
        }
        
        return true
    }

    func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        if MCESdk.shared.config.geofenceEnabled
        {
            let sync = MCELocationClient()
            sync.fetchCompletionHandler = completionHandler
            sync.scheduleSync()
        }
        else
        {
            completionHandler(UIBackgroundFetchResult.noData)
        }
    }
    
    func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
        MCELocationClient().handleEvents(forBackgroundURLSession: identifier, completionHandler: completionHandler)
    }
    
    func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
        MCEEventService.shared.sendPushEnabledEvent()
    }
    
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        MCESdk.shared.registerDeviceToken(deviceToken)
        if #available(iOS 8.0, *) {
            // Unneeded in iOS 8
        }
        else
        {
            MCEEventService.shared.sendPushEnabledEvent()
        }
    }
    
    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        MCEEventService.shared.sendPushEnabledEvent()
        MCESdk.shared.deviceTokenRegistartionFailed()
    }
    
    func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, for notification: UILocalNotification, withResponseInfo responseInfo: [AnyHashable : Any], completionHandler: @escaping () -> Void) {
        MCEInAppManager.shared.processPayload(notification.userInfo)
        MCESdk.shared.processDynamicCategoryNotification(notification.userInfo, identifier: identifier, userText: responseInfo[UIUserNotificationActionResponseTypedTextKey] as! String!)
    }
    
    func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [AnyHashable : Any], withResponseInfo responseInfo: [AnyHashable : Any], completionHandler: @escaping () -> Void)
    {
        MCEInAppManager.shared.processPayload(userInfo)
        completionHandler()
    }
    
    func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [AnyHashable : Any], completionHandler: @escaping () -> Void) {
        
        MCEInAppManager.shared.processPayload(userInfo)
        MCESdk.shared.processCategoryNotification(userInfo, identifier: identifier)
        completionHandler()
    }
    
    
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
        MCEInAppManager.shared.processPayload(userInfo)
        MCESdk.shared.presentOrPerformNotification(userInfo)
    }
    
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        MCEInAppManager.shared.processPayload(userInfo)
        MCESdk.shared.presentDynamicCategoryNotification(userInfo)
        completionHandler(UIBackgroundFetchResult.newData)
    }
    
    func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
        MCESdk.shared.presentOrPerformNotification(notification.userInfo)
    }
    
    func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, for notification: UILocalNotification, completionHandler: @escaping () -> Void) {
        MCESdk.shared.processDynamicCategoryNotification(notification.userInfo, identifier: identifier, userText: nil)
        completionHandler()
    }

}

Go Back to the Mobile App Messaging home page.

Join The Discussion

Your email address will not be published. Required fields are marked *