Customer Engagement
Using the Netcore CE dashboard, 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 9 steps process
Step 1: Complete basic setup
Go to Basic Setup to start integrating your iOS project with Netcore CE SDK.
Step 2: Adding SmartPush SDK dependency - CocoaPods
- In the
Podfile
add theSmartPush-iOS-SDK
as a dependency.
pod 'SmartPush-iOS-SDK', '~> <<push_sdk_ios_version>>'
- Run the following command to update the added pod in your
Podfile
.
pod install
Once your installation is complete, you will have to open the workspace file (YOUR-PROJECT-NAME.xcworkspace) that is created.
- Import SmartPush SDK in AppDelegate class.
#import <SmartPush/SmartPush.h>
import SmartPush
Step 3: Adding & Configuring of Extensions
Click here to add & configure extensions.
Step 4: Register For Remote Notifications
In order to receive push notifications you need to send the device token you received in method didRegisterForRemoteNotificationsWithDeviceToken to Netcore CE . Refer the following code:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[[SmartPush sharedInstance] didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
SmartPush.sharedInstance().didRegisterForRemoteNotifications(withDeviceToken: deviceToken)
}
For an error/failure for registering of remote notification.
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
[[SmartPush sharedInstance] didFailToRegisterForRemoteNotificationsWithError:error];
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
SmartPush.sharedInstance().didFailToRegisterForRemoteNotificationsWithError(error)
}
Step 5: 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 {
[[SmartPush sharedInstance] willPresentForegroundNotification:notification];
completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
[[SmartPush sharedInstance] didReceiveNotificationResponse:response];
completionHandler();
}
//MARK:- UNUserNotificationCenterDelegate Methods
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
SmartPush.sharedInstance().willPresentForegroundNotification(notification)
completionHandler([.alert, .badge, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
SmartPush.sharedInstance().didReceive(response)
}
completionHandler()
}
Step 6: Set Notification Options
Netcore CE 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 {
//...
[[SmartPush sharedInstance] registerForPushNotificationWithDefaultAuthorizationOptions];
//...
return YES;
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//...
SmartPush.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 {
//...
[[SmartPush sharedInstance] registerForPushNotificationWithAuthorizationOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound)];
//...
return YES;
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//...
SmartPush.sharedInstance().registerForPushNotification(authorizationOptions: [.alert, .badge, .sound])
//...
return true
}
Step 7: Silent Push Notification
Implement didReceiveRemoteNotification method for Silent Push Notification (3.0.2 onwards)
This will be used for scheduled push notification feature.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler {
[[SmartPush sharedInstance] didReceiveRemoteNotification:userInfo withCompletionHandler:completionHandler];
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
SmartPush.sharedInstance().didReceiveRemoteNotification(userInfo, withCompletionHandler: completionHandler)
}
Step 8: 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 - Smartech Delegate Method
- (void)handleDeeplinkActionWithURLString:(NSString *)deeplinkURLString andNotificationPayload:(NSDictionary *_Nullable)notificationPayload {
NSLog(@"SMTLogger: handleDeeplinkActionWithURLString");
NSLog(@"SMTLogger: Deeplink URL: %@", deeplinkURLString);
NSLog(@"SMTLogger: Notification Payload: %@", notificationPayload);
}
// MARK: - Smartech Delegate Methods
func handleDeeplinkAction(withURLString deeplinkURLString: String, andNotificationPayload notificationPayload: [AnyHashable : Any]?) {
print("SMTLogger: handleDeeplinkActionWithURLString")
print("SMTLogger: Deeplink URL: \(deeplinkURLString)")
print("SMTLogger: NotificationPayload: \(String(describing: notificationPayload))")
}
Step 9: 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.)
- 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.
- Background Modes - Enable/Add Background Modes and select Remote notifications. This allows the app to receive remote notifications when it is in the background.
- Push Notifications - Enable/Add Push Notifications.
Step 10: 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 11: APNs key setup
Click here for steps to create APNs key.
Testing Push Notifications
- 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.
- 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 1 month ago