caTea 블로그

ios10 push notification 세팅 본문

macos | ios

ios10 push notification 세팅

ZaRas 2016. 10. 14. 15:26
반응형


ios10부터 클라이언트에서 apns 등록요청이 달라졌다 하여 하위호완성을 고려해 10버전일경우 분기처리하였다




APPDELEGATE HEADER

#import <UserNotifications/UserNotifications.h>


@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate


--------------------------------------------------------------------------------------------------------------------------

APPDELEGATE.m



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

.

.

.

.

// APNS 등록 10ver 이상

    if([[UIDevice currentDevice] systemVersion].floatValue >= 10){

        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

        center.delegate = self;

        [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){

            if( !error ){

                [[UIApplication sharedApplication] registerForRemoteNotifications];

            }

            else {

                NSLog( @"Push registration FAILED" );

                NSLog( @"ERROR: %@ - %@", error.localizedFailureReason, error.localizedDescription );

                NSLog( @"SUGGESTIONS: %@ - %@", error.localizedRecoveryOptions, error.localizedRecoverySuggestion );

            }

        }];

    } else { //10ver 미만

        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

        [[UIApplication sharedApplication] registerForRemoteNotifications];

        

        if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {

            NSLog(@"Requesting permission for push notifications..."); // iOS 8

            UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:

                                                    UIUserNotificationTypeAlert | UIUserNotificationTypeBadge |

                                                    UIUserNotificationTypeSound categories:nil];

            [application registerUserNotificationSettings:settings];

        }

    }

.

.

.


}



#pragma mark -

#pragma mark Apple Push Notification Handler

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken deviceToken = %@", deviceToken);

}


- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {

NSLog(@"\n\n\nFailed to get PUSH notification=%@\n\n\n",error);

}


- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo  {

  // 10버전 미만일경우 이곳으로 푸시메세지가 온다 단 앱이 실행중일떄다

}


#pragma mark - 

#pragma mark push ios10 ver


-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{

    

    //10버전 이상일경우 이곳으로 메세지가 온다

    

    completionHandler(UNNotificationPresentationOptionAlert); //푸시 배너를 띄운다.

    

    NSLog(@"userNotificationCenter push data = %@",notification.request.content.userInfo);


}


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

    

    //앱이 백그라운드로 살아있고 푸시가 올떄 배너로 앱 진입시 이곳으로 들어온다.

    

    NSLog(@"userNotificationCenter Userinfo %@",response.notification.request.content.userInfo);

    

}

728x90