These docs are for v1.0. Click to read the latest docs for v2.0.

Initialise Smartech SDK

Initialising Smartech SDK

Initialising Smartech SDK

  1. Import Smartech SDK in AppDelegate class.
#import <Smartech/Smartech.h>
import Smartech
  1. Call a method to initialize Smartech SDK in the didFinishLaunchingWithOptions method.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //...
    [[Smartech sharedInstance] initSDKWithDelegate:self];
    //...
    return YES;
}

// 3.0.2 Onwards initialize Smartech SDK in the didFinishLaunchingWithOptions method.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //...
    [[Smartech sharedInstance] initSDKWithDelegate:(id)self withLaunchOptions:launchOptions];
    //...
    return YES;
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:[UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  //...
  Smartech.sharedInstance().initSDK(with: self)
  //...
  return true
}

// 3.0.2 Onwards initialize Smartech SDK in the didFinishLaunchingWithOptions method.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:[UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  //...
   Smartech.sharedInstance().initSDK(with: self, withLaunchOptions: launchOptions)
  //...
  return true
}

Adding methods for Push Notification in AppDelegate class

Register For Remote Notifications

In order to receive push notifications you need to send the device token you received in method didRegisterForRemoteNotificationsWithDeviceToken to Smartech. Refer the following code:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  [[Smartech sharedInstance] didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  Smartech.sharedInstance().didRegisterForRemoteNotifications(withDeviceToken: deviceToken)
}

For an error/failure for registering of remote notification.

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  [[Smartech sharedInstance] didFailToRegisterForRemoteNotificationsWithError:error];
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
  Smartech.sharedInstance().didFailToRegisterForRemoteNotificationsWithError(error)
}

Implementation of UNUserNotificationCenterDelegate methods

Implement the following code in delegate methods of UNUserNotificationCenter. First import following frameworks provided by apple for UNUserNotificationCenterDelegate

#import <UserNotifications/UserNotifications.h>
#import <UserNotificationsUI/UserNotificationsUI.h>
import UserNotifications
import UserNotificationsUI

Confirm UNUserNotificationCenterDelegate in AppDelegate class

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  
  [[Smartech sharedInstance] initSDKWithDelegate:self];
  [UNUserNotificationCenter currentNotificationCenter].delegate = self;
  
  return YES;
}

//Confirming UNUserNotificationCenterDelegate protocol

@interface AppDelegate () <UNUserNotificationCenterDelegate>

@end
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  
  Smartech.sharedInstance().initSDK(with: self)
  UNUserNotificationCenter.current().delegate = self
  
  return true
}

//Confirming UNUserNotificationCenterDelegate protocol

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

}

Implement UNUserNotificationCenterDelegate methods in AppDelegate class

#pragma mark - UNUserNotificationCenterDelegate Methods
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
  [[Smartech sharedInstance] willPresentForegroundNotification:notification];
  completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
  [[Smartech sharedInstance] didReceiveNotificationResponse:response];
  completionHandler();
}
//MARK:- UNUserNotificationCenterDelegate Methods
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
  Smartech.sharedInstance().willPresentForegroundNotification(notification)
  completionHandler([.alert, .badge, .sound])
}
    
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
  Smartech.sharedInstance().didReceive(response)
  completionHandler()
}

Set Notification Options

Smartech SDK has a default method that sets the notification options with options Sound, Alert, Badge. For that, you need to call the method registerForPushNotificationWithDefaultAuthorizationOptions in AppDelegate class.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  //...
  [[Smartech sharedInstance] initSDKWithDelegate:self];
  [[Smartech sharedInstance] registerForPushNotificationWithDefaultAuthorizationOptions];
  //...
  return YES;
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  //...
  Smartech.sharedInstance().initSDK(withDelegate: self)
  Smartech.sharedInstance().registerForPushNotificationWithDefaultAuthorizationOptions()
  //...
  return true
}

If you want to change options you can use the following method named setNotificationOptions to set the notification options in AppDelegate class in didFinishLaunchingWithOptions method.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  //...
  [[Smartech sharedInstance] initSDKWithDelegate:self];
  [[Smartech sharedInstance] registerForPushNotificationWithAuthorizationOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound)];
  //...
  return YES;
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  //...
  Smartech.sharedInstance().initSDK(withDelegate: self)
  Smartech.sharedInstance().registerForPushNotification(authorizationOptions: [.alert, .badge, .sound])
  //...
  return true
}

Check logs of Smartech SDK

To check the logs of Smartech SDK, you need to implement a method named setDebugLevel in AppDelegate class in method didFinishLaunchingWithOptions.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  //...
  [[Smartech sharedInstance] initSDKWithDelegate:self];
  [[Smartech sharedInstance] registerForPushNotificationWithDefaultAuthorizationOptions];
  [[Smartech sharedInstance] setDebugLevel:SMTLogLevelVerbose];
  //...
  return YES;
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  //...
  Smartech.sharedInstance().initSDK(withDelegate: self)
  Smartech.sharedInstance().registerForPushNotificationWithDefaultAuthorizationOptions()
  Smartech.sharedInstance().setDebugLevel(.verbose)
  //...
  return true
}

🚧

Note:

It is recommended to change the log level to SMT_LOG_LEVEL_NONE before uploading the app to the App Store.

Silent Push Notification

Implement didReceiveRemoteNotification method for Silent Push Notification (3.0.2 onwards)

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler {
   [[Smartech sharedInstance] didReceiveRemoteNotification:userInfo withCompletionHandler:^(BOOL finishedScheduling) {
        if (finishedScheduling) {
            completionHandler(UIBackgroundFetchResultNewData);
        }
        else {
            completionHandler(UIBackgroundFetchResultNoData);
        }
    }];
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
      Smartech.sharedInstance().didReceiveRemoteNotification(userInfo, withCompletionHandler: completionHandler)

    }

Handle Deep Link and Custom Payload

To handle URL/Deeplink/Universal Link and custom payload the developer needs to implement one delegate method named handleDeeplinkActionWithURLString of SmartechDelegate in AppDelegate.m which will provide the URL/Deeplink/Universal Link and custom payload.
Implement SmartechDelegate and confirm the protocol of it.

@interface AppDelegate () <UNUserNotificationCenterDelegate, SmartechDelegate>

@end
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, SmartechDelegate {

}

Implement SmartechDelegate method

#pragma mark - SmartechDelegate Method
- (void)handleDeeplinkActionWithURLString:(NSString *)deeplinkURLString andCustomPayload:(NSDictionary *_Nullable)customPayload {
  //...
  NSLog(@"Deeplink: %@", deeplinkURLString);
  NSLog(@"Custom Payload: %@", customPayload);
  //...
}
//MARK:- SmartechDelegate Method
func handleDeeplinkAction(withURLString deeplinkURLString: String, andCustomPayload customPayload: [AnyHashable : Any]?) {
  //...
  print("Deeplink: \(deeplinkURLString)")
  if customPayload != nil {
    print("Custom Payload: \(customPayload!)")
  }
  //...
}

Verify AppDelegate code from below

#import "AppDelegate.h"

#import <UserNotifications/UserNotifications.h>
#import <UserNotificationsUI/UserNotificationsUI.h>
#import <Smartech/Smartech.h>
  
@interface AppDelegate () <UNUserNotificationCenterDelegate, SmartechDelegate>

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  
  [[Smartech sharedInstance] initSDKWithDelegate:self];
  [[Smartech sharedInstance] registerForPushNotificationWithDefaultAuthorizationOptions];
  [[Smartech sharedInstance] setDebugLevel:SMTLogLevelVerbose];
  [UNUserNotificationCenter currentNotificationCenter].delegate = self;
  
  return YES;
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  [[Smartech sharedInstance] didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  [[Smartech sharedInstance] didFailToRegisterForRemoteNotificationsWithError:error];
}

#pragma mark - UNUserNotificationCenterDelegate Methods
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
  [[Smartech sharedInstance] willPresentForegroundNotification:notification];
  completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
  [[Smartech sharedInstance] didReceiveNotificationResponse:response];
  completionHandler();
}

#pragma mark - SmartechDelegate Method
- (void)handleDeeplinkActionWithURLString:(NSString *)deeplinkURLString andCustomPayload:(NSDictionary *_Nullable)customPayload {
  //...
  NSLog(@"Deeplink: %@", deeplinkURLString);
  NSLog(@"Custom Payload: %@", customPayload);
  //...
}

@end
import UIKit
import UserNotifications
import UserNotificationsUI
import Smartech

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, SmartechDelegate {
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:[UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        //...
        Smartech.sharedInstance().initSDK(with: self)
        Smartech.sharedInstance().registerForPushNotificationWithDefaultAuthorizationOptions()
        Smartech.sharedInstance().setDebugLevel(.verbose)
        UNUserNotificationCenter.current().delegate = self
        //...
        return true
    }
    
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        Smartech.sharedInstance().didRegisterForRemoteNotifications(withDeviceToken: deviceToken)
    }
    
    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        Smartech.sharedInstance().didFailToRegisterForRemoteNotificationsWithError(error)
    }
    
    //MARK:- UNUserNotificationCenterDelegate Methods
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        Smartech.sharedInstance().willPresentForegroundNotification(notification)
        completionHandler([.alert, .badge, .sound])
    }
    
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        Smartech.sharedInstance().didReceive(response)
        completionHandler()
    }
    
    //MARK:- SmartechDelegate Method
    func handleDeeplinkAction(withURLString deeplinkURLString: String, andCustomPayload customPayload: [AnyHashable : Any]?) {
        print("Deeplink: \(deeplinkURLString)")
        if customPayload != nil {
            print("Custom Payload: \(customPayload!)")
        }
    }
}

Identifying Smartech Notifications

if ([[Smartech sharedInstance] isNotificationFromSmartech:(nonnull NSDictionary *)]) {
  
}
if Smartech.sharedInstance().isNotification(fromSmartech: [AnyHashable : Any]) {
  
}

Configuring Capabilities and Info.plist of App

Configuring Capabilities of App

Enabling Capabilities for App

📘

Note:

  1. Make sure you have added App Transport Security Settings with Allow Arbitrary Loads as YES in Info.plist of App, Service Extension, and Content Extension to support Http calls.
  2. App Groups must be same between App, Service Extension, and Content Extension.

For Smartech SDK to work in your project you need to enable below app capabilities in your main app.

  1. App Groups - Enable/Add and select a valid App Group for your app. This is used by App, Service Extension and Content Extension to access data as all three are different targets.
  2. Background Modes - Enable/Add Background Modes and select Remote notifications. This used to send remote notifications in background mode.
  3. Push Notifications - Enable/Add and Configure App Push Notifications feature for your App ID in Apple Developer Account. This is used to enable the push notifications for the app.

Configure Apps Info.plist with SmartechKeys

You need to add a key named SmartechKeys with type Dictionary. Init add the following keys and values with their types.

KeyData typeDescription
SmartechAppGroupStringThe app group that is selected in capabilities section. It is used to share data between App and Extensions.
SmartechAppIdStringThe app id received from Smartech panel under assets section.
SmartechAutoFetchLocationBooleanThis key decides whether you want to automatically send the location with all the events or not. Set value as true/false as per the requirement.
SmartechUseAdvIdBooleanThis key decides whether to use ADID (Advertising Id) or not. Set value as true/false as per the requirement.

Following is the source code snippet of apps Info.plist for key SmartechKeys:

<key>SmartechKeys</key>
	<dict>
		<key>SmartechAppGroup</key>
		<string>group.com.CompanyName.ProductName</string>
		<key>SmartechAppId</key>
		<string>abcdef123456abcdef123456abcd1234</string>
		<key>SmartechUseAdvId</key>
		<true/>
    	<key>SmartechAutoFetchLocation</key>
		<true/>
	</dict>

Following is the image representation of apps Info.plist for key SmartechKeys:

692