Tuesday 11 October 2016

Push Notifications for iOS 10 code in objective-c

Step by step guide to implement push notification in iOS10


Step 1 : Import UserNotifications.framework in your AppDelegate.h file
#import <UserNotifications/UserNotifications.h>
Setp 2 : Add "UNUserNotificationCenterDelegate"
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
@end

Step 3 :  Register for push notification in AppDelegate.m file
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UNUserNotificationCenter *notificationCenter = [UNUserNotificationCenter currentNotificationCenter];
notificationCenter.delegate = self;
[notificationCenter requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
if( !error ){
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
}];

return YES;
}
Step 4 :  Now time to implement Notification Delegate methods in AppDelegate.m file.-

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
NSLog(@"User Info = %@",notification.request.content.userInfo);
completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound);
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{

NSLog(@"User Info = %@",response.notification.request.content.userInfo);
completionHandler();
}








No comments:

Post a Comment