Other changes 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)
}
Handle Notifications
To handle local notifications in OS version below 10.0 implement the following code in application:didReceiveLocalNotification:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
[[Smartech sharedInstance] didReceiveLocalNotification:notification.userInfo];
}
func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
Smartech.sharedInstance().didReceiveLocalNotification(notification.userInfo!)
}
To handle notifications in OS version above 10.0 implement the following code in delegate methods of UNUserNotificationCenter
First import following frameworks provided by apple for UNUserNotificationCenter
#import <UserNotifications/UserNotifications.h>
#import <UserNotificationsUI/UserNotificationsUI.h>
import UserNotifications
import UserNotificationsUI
Delegate methods to be used of UNUserNotificationCenter:
- userNotificationCenter:willPresentNotification:withCompletionHandler
- userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler
To implement these methods you add following lines in didFinishLaunchingWithOptions
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[Smartech sharedInstance] initSDKWithDelegate:self withLaunchOptions:launchOptions];
if (@available(iOS 10.0, *)) {
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
}
return YES;
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
Smartech.sharedInstance().initSDK(withDelegate: self, withLaunchOptions: launchOptions)
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self
}
return true
}
Defining the UNUserNotificationCenterDelegate protocol in AppDelegate
@interface AppDelegate () <UNUserNotificationCenterDelegate>
@end
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
}
Implementing netcore methods in UNUserNotificationCenterDelegate methods
#pragma mark - UNUserNotificationCenter Delegate Methods
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler API_AVAILABLE(ios(10.0)) {
[[Smartech sharedInstance] willPresentForegroundNotification:notification];
completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler API_AVAILABLE(ios(10.0)) {
[[Smartech sharedInstance] didReceiveNotificationResponse:response];
completionHandler();
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
Smartech.sharedInstance().willPresentForegroundNotification(notification)
completionHandler([.alert, .badge, .sound])
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
Smartech.sharedInstance().didReceive(response)
completionHandler()
}
Check logs of Netcore SDK
To check the logs of Netcore 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 withLaunchOptions:launchOptions];
[[Smartech sharedInstance] setDebugLevel:NCLogLevelVerbose];
//...
return YES;
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//...
Smartech.sharedInstance().initSDK(with: self, withLaunchOptions: launchOptions)
Smartech.sharedInstance().setDebugLevel(.verbose)
//...
return true
}
Note:
It is recommended to change the log level to NCLogLevelNone before uploading the app to the App Store.
Verify full code of AppDelegate with Smartech SDK
#import "AppDelegate.h"
#import <NetCorePush/NetCorePush.h>
@interface AppDelegate () <UNUserNotificationCenterDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[Smartech sharedInstance] initSDKWithDelegate:self withLaunchOptions:launchOptions];
[[Smartech sharedInstance] setDebugLevel:NCLogLevelVerbose];
if (@available(iOS 10.0, *)) {
[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];
}
//Method for supporting os version below 10
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
[[Smartech sharedInstance] didReceiveLocalNotification:notification.userInfo];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler {
[[Smartech sharedInstance] didReceiveRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
#pragma mark - UNUserNotificationCenterDelegate Methods
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler API_AVAILABLE(ios(10.0)) {
[[Smartech sharedInstance] willPresentForegroundNotification:notification];
completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler API_AVAILABLE(ios(10.0) {
[[Smartech sharedInstance] didReceiveNotificationResponse:response];
completionHandler();
}
@end
import UIKit
import UserNotifications
import UserNotificationsUI
import NetCorePush
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
Smartech.sharedInstance().initSDK(with: self, withLaunchOptions: launchOptions)
Smartech.sharedInstance().setDebugLevel(.verbose)
if #available(iOS 10.0, *) {
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)
}
//Method for supporting os version below 10
func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
Smartech.sharedInstance().didReceiveLocalNotification(notification.userInfo!)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
Smartech.sharedInstance().didReceiveRemoteNotification(userInfo)
completionHandler(.newData)
}
//MARK:- UNUserNotificationCenterDelegate
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
Smartech.sharedInstance().willPresentForegroundNotification(notification)
completionHandler([.alert, .badge, .sound])
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
Smartech.sharedInstance().didReceive(response)
completionHandler()
}
}
Updated over 4 years ago