Summary

APNs 를 사용하여 푸쉬 서비스를 이용할 경우 소스코드 내에 푸쉬 서비스 등록 및 푸쉬를 받았을 경우 처리할 수 있는 delegate 를 구현해 주어야 한다.
iOS10 부터 UNUserNotification 을 사용하여 서비스 등록 및 처리 방법이 변경되었기 때문에 iOS10 버전과 하위버전을 같이 처리할 수 있도록 적용해 주어야 한다.

푸시 서비스 등록 및 처리

/************
AppDelegate.h
*************/
#import <UserNotifications/UserNotifications.h> // ( >= iOS10)
// UNUserNotificationCenterDelegate
@interface AppDelegate : UIResponder <UIApplicationDelegate, UNUserNotificationCenterDelegate>
...
/************
AppDelegate.m
*************/
...
#define isOSVersionOver10 ([[[[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."] objectAtIndex:0] integerValue] >= 10)
...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
//
[self initializeRemoteNotification];
...
}
#pragma mark - Initialize Remote Notification
- (void)initializeRemoteNotification {
if(isOSVersionOver10) { // iOS10
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
if( !error ) {
//
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else {
//
}];
}
else { // iOS10
if ([[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
}
}
#pragma mark - Get Device Token
//
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSMutableString *tokenHex = [NSMutableString stringWithString:[deviceToken description]];
[tokenHex replaceOccurrencesOfString:@"<" withString:@"" options:0 range:NSMakeRange(0, [tokenHex length])];
[tokenHex replaceOccurrencesOfString:@">" withString:@"" options:0 range:NSMakeRange(0, [tokenHex length])];
[tokenHex replaceOccurrencesOfString:@" " withString:@"" options:0 range:NSMakeRange(0, [tokenHex length])];
NSLog(@"Token origin : %@", deviceToken);
NSLog(@"Token : %@", tokenHex);
}
// iOS9 Delegate
#pragma mark - Remote Notification Delegate for <= iOS 9.x
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
[application registerForRemoteNotifications];
}
//
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"Remote notification : %@",userInfo);
}
//
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"Error : %@",error);
}
// iOS10 Delegate
#pragma mark - UNUserNotificationCenter Delegate for >= iOS 10
//
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
NSLog(@"Remote notification : %@",notification.request.content.userInfo);
//
completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound);
}
//
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
NSLog(@"Remote notification : %@",response.notification.request.content.userInfo);
completionHandler();
}
iOS10 부터는 소스코드 추가 이외에 프로젝트 설정에서 Push Notificaiton 옵션을 활성화 해주어야 한다


옵션을 활성화 해주면 아래와 같이 entitlements 파일이 생성될 것이다.

이렇게 해주면 모든 푸시 서비스 등록 작업이 마무리된다.