Skip to content

Handle In-app Content

This setup allows NearIT to deliver and show notifications and content.

Notification Setup

When NearIT delivers content it uses system notifications.

Starting from iOS10 you can detect notifications being tapped or being shown while the app is open, implementing the userNotificationCenter(_:didReceive:withCompletionHandler:) and userNotificationCenter(_:willPresent:withCompletionHandler:) methods of your notification delegate and then send the content to your handleNearContent method.

With the following snippet, if your app is closed or in background, a system notification will be added to the Notification Center. If your app is in foreground, the notification will be shown inside the app.

// AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    ...
    if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
            // Enable or disable features based on authorization.
        }
        UNUserNotificationCenter.current().delegate = self
    }   
}

...
@available(iOS 10.0, *)
extension AppDelegate : UNUserNotificationCenterDelegate {

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler(.alert)
        NearManager.shared.update(with: notification)
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        let isNearNotification = NearManager.shared.processRecipeFrom(response) { (content, trackingInfo, error) in
            if let content = content, let trackingInfo = trackingInfo {
                self.handleNearContent(content: content, trackingInfo: trackingInfo)
            }
        }
        completionHandler()
    }
}
// Declare a UNUserNotificationCenterDelegate
@interface AppDelegate()<UNUserNotificationCenterDelegate>;

@end

// Set the delegate in didFinishLaunching
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [UNUserNotificationCenter currentNotificationCenter].delegate = self;
    [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {
        // Enable or disable features based on authorization.
    }];
    return YES;
}

// UNUserNotificationCenterDelegate implementation
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
    completionHandler(UNNotificationPresentationOptionAlert);
    [NITManager.defaultManager updateWithNotification:notification];
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
    BOOL isNearNotification = [[NITManager defaultManager] processRecipeWithResponse:response completion:^(NITReactionBundle * _Nullable content, NITTrackingInfo * _Nullable trackingInfo, NSError * _Nullable error) {
        if (content) {
            [self handleNearContent:content trackingInfo: trackingInfo];
        }
    }];
}

If you want to customize your notifications, see this section.

If you want to achieve the same results for iOS9, you can checkout our snippets in this section

Handle the content

If you followed the previous section, you will have all NearIT content delivered to a handleNearContent method. Use this implementation to automatically show a content modal in most situations:

func handleNearContent(_ content: Any, trackingInfo: NITTrackingInfo? = nil) {
    if let content = content as? NITContent {
        let contentVC = NITContentViewController(content: content)
        contentVC.show()
    } else if let feedback = content as? NITFeedback {
        let feedbackVC = NITFeedbackViewController(feedback: feedback)
        feedbackVC.show()
    } else if let coupon = content as? NITCoupon {
        let couponVC = NITCouponViewController(coupon: coupon)
        couponVC.show()
    } else if let simple = content as? NITSimpleNotification {
        // there's no content attached to the system notification that was just pressed
    } else if let customJson = content as? NITCustomJSON {
        // handle your custom json
    }
}
- (void)handleNearContent:(NITReactionBundle*)aContent trackingInfo:(NITTrackingInfo*)trackingInfo {
    if ([aContent isKindOfClass:[NITContent class]]) {
        NITContent *content = aContent;
        NITContentViewController *vc = [[NITContentViewController alloc] initWithContent:content trackingInfo:trackingInfo];
        [vc show];
    } else if ([aContent isKindOfClass:[NITFeedback class]]) {
        NITFeedback *feedback = aContent;
        NITFeedbackViewController *vc = [[NITFeedbackViewController alloc] initWithFeedback:feedback];
        [vc show];
    } else if ([aContent isKindOfClass:[NITCoupon class]]) {
        NITCoupon *coupon = aContent;
        NITCouponViewController *vc = [[NITCouponViewController alloc] initWithCoupon:coupon];
        [vc show];
    } else if ([aContent isKindOfClass:[NITSimpleNotification class]]) {
        NITSimpleNotification *simpleContent = aContent;
        // there's no content attached to the system notification that was just pressed
    } else if ([aContent isKindOfClass:[NITCustomJSON class]]) {
        NITCustomJSON *customJSON = aContent;
        // handle your custom json
    }
}

Notification History

If you want to show the user notification history, you can show it with our view controller:

let historyVC = NITNotificationHistoryViewController()
historyVC.show()
NITNotificationHistoryViewController *historyVC = [[NITNotificationHistoryViewController alloc] init];
[historyVC show];

or you can show it in your UINavigationController:

let historyVC = NITNotificationHistoryViewController()
historyVC.show(navigationController: rootNavController!)
NITNotificationHistoryViewController *historyVC = [[NITNotificationHistoryViewController alloc] init];
[historyVC showWithNavigationController:rootNavController];

This is the result:

Inbox list

If you want to customize the behaviour and look of our UIs or to manually handle the user notification history, head over the notification history section.

If you want to get notified when new content is available (useful if you want to display a badge), you can register for messages updates. See the section for more info.

Coupon History

If you want to show the user coupon list, you can show it with our view controller:

let vc = NITCouponListViewController()
vc.show()
NITCouponListViewController *vc = [[NITCouponListViewController alloc] init];
[vc show];

This is the result:

Coupon list Coupon detail

If you want to customize the behaviour and look of our UI or to manually handle the user coupons, head over to the coupon section