Log In

This section describes how to integrate MOCA iOS SDK with your app code.

"Show me the code" Integration

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        let mocaStarted = MOCA.initializeSDK()
        if !mocaStarted {
            print("Cannot initialize MOCA SDK")
        }
        let center = UNUserNotificationCenter.current()
        center.delegate = self
        return true
    }
    
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        if(MOCA.initialized() && MOCA.isMocaNotification(notification)) {
            MOCA.userNotificationCenter(center, willPresent: notification, withCompletionHandler: completionHandler)
        }

    }
    
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        if(MOCA.initialized() && MOCA.isMocaNotification(response)) {
            MOCA.userNotificationCenter(center, didReceive: response, withCompletionHandler: completionHandler)
        }
    }
    
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
     if MOCA.initialized() {
         MOCA.handleRemoteNotification(userInfo, fetchCompletionHandler: completionHandler)
     }
    }

     func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
         print("deviceToken: @%", deviceToken)
         if MOCA.initialized() {
             MOCA.registerDeviceToken(deviceToken)
         }
     }
     
     func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
         print("remote push notifications registration failed")
     }
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  // do not forget to #import "MOCA.h"
    BOOL mocaStarted = [MOCA initializeSDK];
    if (!mocaStarted) {
        NSLog(@"Cannot initialize MOCA SDK");
    }
	  // do not forget declare that your AppDelegate conforms with the         UNUserNotificationCenterDelegate protocol
    UNUserNotificationCenter *notificationCenter = [UNUserNotificationCenter currentNotificationCenter];
    notificationCenter.delegate = self;

    return YES;
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
    if (MOCA.initialized && [MOCA isMocaNotification:notification]) {
        [MOCA userNotificationCenter:center
             willPresentNotification:notification
               withCompletionHandler:completionHandler];
    }
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void (^)(void))completionHandler {
    if (MOCA.initialized && [MOCA isMocaNotification:response]) {
        [MOCA userNotificationCenter:center
      didReceiveNotificationResponse:response
               withCompletionHandler:completionHandler];
    }
}

- (void)         application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
      fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    if (MOCA.initialized && [MOCA isMocaNotification:userInfo]) {
        [MOCA handleRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
    }
}

- (void)                             application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"deviceToken: %@", deviceToken);
    if ([MOCA initialized]) {
    	[MOCA registerDeviceToken:deviceToken];
    }
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"Failed to get token, error: %@", error);
}

Step by step integration

1 - (Objective-C Only) Import "MOCA.h" header file into your app’s delegate implementation file.

2 - initialize MOCA SDK is in you app’s delegate application:didFinishLaunchingWithOptions: method.

#import <MOCA.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Initialize MOCA SDK.
    BOOL mocaReady = [MOCA initializeSDK];
    if (!mocaReady) {
        NSLog(@"MOCA SDK initialization failed.");
    }
    return YES;
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        let mocaStarted = MOCA.initializeSDK()
        if !mocaStarted {
            print("Cannot initialize MOCA SDK")
        }
        return true
    }
  • You must initialize the SDK before calling any other method.

  • On initialization, MOCA SDK will load configuration from MOCAConfig.plist file and perform all necessary framework setup.

  • The [MOCA initializeSDK] method call returns YES on success, and NO on error.

3 - Handle local notifications in your AppDelegate:

3.1 - Subscribe to the Notification Center. This is typically done in the AppDelegate. Remember to have only a single delegate in your app.

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
let center = UNUserNotificationCenter.current()
center.delegate = self

3.2 - Declare the protocol conformance in your class. Example:

@interface AppDelegate : <UIApplicationDelegate,        UNUserNotificationCenterDelegate>
class AppDelegate: ...  UNUserNotificationCenterDelegate

3.3 - Implement the following methods in the class that conforms with the UNUserNotificationCenterDelegate protocol:

// The method will be called on the delegate only if the application is in the foreground. If the method is not implemented or the handler is not called in a timely manner then the notification will not be presented. The application can choose to have the notification presented as a sound, badge, alert and/or in the notification list. This decision should be based on whether the information in the notification is otherwise visible to the user.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
    if(MOCA.initialized && [MOCA isMocaNotification:notification]) {
        [MOCA userNotificationCenter:center willPresentNotification:notification withCompletionHandler:completionHandler];
    }
}

// The method will be called on the delegate when the user responded to the notification by opening the application, dismissing the notification or choosing a UNNotificationAction. The delegate must be set before the application returns from application:didFinishLaunchingWithOptions:.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void(^)(void))completionHandler {
    if(MOCA.initialized && [MOCA isMocaNotification:response]) {
        [MOCA userNotificationCenter:center didReceiveNotificationResponse:response withCompletionHandler:completionHandler];
    }
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        if(MOCA.initialized() && MOCA.isMocaNotification(notification)) {
            MOCA.userNotificationCenter(center, willPresent: notification, withCompletionHandler: completionHandler)
        }

    }
    
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        if(MOCA.initialized() && MOCA.isMocaNotification(response)) {
            MOCA.userNotificationCenter(center, didReceive: response, withCompletionHandler: completionHandler)
        }
    }

4 - Integrate with iOS Remote Push Notifications

-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler: (void (^)(UIBackgroundFetchResult))completionHandler {
   if (MOCA.initialized) {
        [MOCA handleRemoteNotification:userInfo fetchCompletionHandler:handler];
    }
}

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSLog(@"deviceToken: %@", deviceToken);
    if(![MOCA initialized]) {
        return;
    }
   [MOCA registerDeviceToken:deviceToken];
}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
    NSLog(@"Failed to get token, error: %@", error);
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
     if MOCA.initialized() {
         MOCA.handleRemoteNotification(userInfo, fetchCompletionHandler: completionHandler)
     }
    }

     func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
         print("deviceToken: @%", deviceToken)
         if MOCA.initialized() {
             MOCA.registerDeviceToken(deviceToken)
         }
     }
     
     func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
         print("remote push notifications registration failed")
     }

❗️

3rd Party SDKs

If you are using 3rd party SDKs or your own application makes use of the Remote or Local notifications. Make sure to filter out not MOCA notifications by calling:

[MOCA isMocaNotification:notification] Obj-C
MOCA.isMocaNotification(notification) Swift

👍

At this point, your application is ready to receive MOCA Mobile Engagement campaigns.
Go to the MOCA Console, create some campaigns and test your MOCA Engagement Experiences.