본문 바로가기
iOS/Objective-C

Push Notification

by 패쓰킴 2021. 12. 6.
728x90

< push 수신 시섬의 앱 상태 >

  1. 앱이 실행상태가 아니다 (push 수신으로 앱 실행)
  2. 실행 중에 push 수신
  3. 백그라운드 상태에서 push 수신

< 앱 상태: UIApplicationState >

  • UIApplicationStateActive: 앱 실행 중
  • UIApplicationStateInactive: 앱이 실행 중이지만 이벤트가 없는 상태(중단 중이거나 백그라운드로 전환 중)
  • UIApplicationStateBackground: 앱이 백그라운드에 있음

< push 알림 허용 상태 확인 >

[[[UIApplication sharedApplication] currentUserNotificationSettings] types] != UIUserNotificationTypeNone
// 결과가 true면 알림 허용 상태

< iOS 버전 확인 >

BOOL isiOS10 = [[[[UIDevice currentDevice] systemVersion componentsSeperateByString@""] objectAtIndex:0] integerValue] >= 10;
또는
[UNUserNotificationCenter class] != nil // iOS 10 부터 사용 가능하므로..

구현

  • iOS 7.0 이후
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  if ([UIApplication sharedApplication].applicationState == UIApplicationStatelnactive) {
    // 3번 상태
  } else {
    // 2번 상태
  }
}
  • iOS 7.0 이전
// launchOptions와 userInfo: push payload 값이 포함된다.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary<UIApplicationLaunchOptionsKey,id> *)launchOptions {
  if (launchOptions && [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]) {
    // 1번 상태
  }
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  if (application.applicationState == UIApplicationStateActive) {
    // 2번 상태
  } else {
    // 3번 상태
  }
}
  • iOS 10 이후
UserNotification Framework 사용

< Notification 관리 >
ClassUNNotificationSettings/ 노티피케이션 세팅 및 인증서 관리UNUserNotificationCenter/ 노티피케이션과 앱 사이의 관리자Protocol: UNUserNotificationCenterDelegate/ 노티피케이션 핸들링

< UserNotification >
iOS 10 이후 등장
기존에는 APNS에서 푸시를 받을 때, 푸시를 보내는 쪽에서 'content-available:1'로 설정할 경우 UIApplicationDelegate 함수인 'application:didReceiveRemoteNotification: fetchCompletion'을 사용했다. 하지만 이 함수는 iOS10 미만의 디바이스에서만 호출된다.
따라서
iOS 10 이후부터는 'userNotificationCenter:willPresentNotification:withCompletionHandler'를 사용

 

 

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

   UNUserNotificationCenter * notiCenter = [UNUserNotificationCenter currentNotificationCenter];

   if ([UNUserNotificationCenter class] != nil) { // iOS 10 이상 버전 처리
     notiCenter.delegate = self;
     [notiCenter requestAuthorizationWithOptions: UNAuthorizationOptionAlert|UNAuthorizationOptionSound|UNAuthorizationOptionBadge completionHandler: nil];
   } else { // iOS 10 미만 버전 처리
     // isRegisteredForRemoteNotifications: UIApplication 인스턴스 속성
     if ([[UIApplication sharedApplication] responsToSelector: @selector(isRegisteredForRemoteNotifications)]) {
       [notiCenter requestAuthorizationWithOptions: UNAuthorizationOptionAlert|UNAuthorizationOptionSound|UNAuthorizationOptionBadge completionHandler: nil];
     }
   }
  [application registerForRemoteNotifications];
}

 

참고:

https://g-y-e-o-m.tistory.com/74

https://tez.kr/158

https://developer.apple.com/documentation/usernotifications

https://developer.apple.com/documentation/uikit/uiapplication/state

https://hansne.wordpress.com/2014/11/13/ios-%EB%B2%84%EC%A0%84%EC%97%90-%EB%94%B0%EB%A5%B8-push-%EC%88%98%EC%8B%A0%ED%95%98%EA%B8%B0/

노티 allinone 참고 - https://itnext.io/swift-local-notification-all-in-one-ee6027ea6e3

728x90

'iOS > Objective-C' 카테고리의 다른 글

스트링 인터닝(String Interning)  (0) 2021.12.15
cornerRadius 원하는 부분에만 적용  (0) 2021.12.15
_myView vs. self.myView  (0) 2021.11.17
arrow notation (`->`)  (0) 2021.11.16
isEqual vs. ==  (0) 2021.11.11

댓글