Smartech

Customer Engagement

Using the Smartech panel, you can engage with your app users via channels like push notifications & in-app messages.

Setting up Push Notifications for your iOS project is a quick 8 steps process

Step 1: Complete basic setup

Go to Basic Setup to start integrating your iOS project with Smartech SDK.

Step 2: Adding & Configuring of Extensions

Click here to add & configure extensions.

Step 3: 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)
}

Step 4: 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 {
  //....
  
  [UNUserNotificationCenter currentNotificationCenter].delegate = self;
  
  //...
  return YES;
}

//Confirming UNUserNotificationCenterDelegate protocol

@interface AppDelegate () <UNUserNotificationCenterDelegate>

@end
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  //...
  
  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()
}

Step 5: 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] registerForPushNotificationWithDefaultAuthorizationOptions];
  //...
  return YES;
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  //...
   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] registerForPushNotificationWithAuthorizationOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound)];
  //...
  return YES;
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  //...
  Smartech.sharedInstance().registerForPushNotification(authorizationOptions: [.alert, .badge, .sound])
  //...
  return true
}

Step 6: Configuring Capabilities of App

Enable the below app capabilities in the targets of your main app. (To know how to add capabilities check this developer document.)

  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 share data with each other. Make sure that you select the same App Group for App, Service Extension, and Content Extension.
  2. Background Modes - Enable/Add Background Modes and select Remote notifications. This allows the app to receive remote notifications when it is in the background.
  3. Push Notifications - Enable/Add Push Notifications.

Step 7: Enable Push Notification for App Id

Configure App Push Notifications feature for your App ID in Apple Developer Account. Check this document for details.

Step 8: APNs key setup

Click here for steps to create APNs key.

📘

Testing Push Notifications

  1. Add 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. Push Notifications can only be tested over a real device and NOT through XCode. You can use any iOS device (iPhone, iPad, iPod Touch) to test the push notifications.

Updated about a month ago


Next

Choose from below options to setup further features of your app's push notifications!

Adding And Configuring of Extensions in your App
Configuring Action buttons
Advanced

Customer Engagement


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.