본문 바로가기
기술/iOS

application:didReceiveRemoteNotification에서 Noti 받을 때 앱 상태별 처리.

by 프리지크 2013. 11. 18.
반응형

iOS에서 Push Notification을 받았을 때 처리하는 방법은 크게 3가지가 있다.


1) 실행 중이 아닌 상태에서 노티를 받았을 때,

    application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 에서 처리.

   

2) 실행 중이며 Foreground 상태 일 때, 

application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo


3) 실행 중이며 Background 상태 일 때, 

application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo


각각의 예를 보자면, 

1)번의 경우, 최초에 한번 실행되는 didFinishLaunchingWithOptions에서 처리.
   예)  

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

{

   NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];


    if(userInfo != nil)

    {

        [self application:application didReceiveRemoteNotification:userInfo];

    }

 

    // ....

}


2), 3)번의 경우에서 Foreground와 Background의 상태에 따라서 처리를 다르게 할려면 UIApplicationState를 이용하면 된다.


typedef NS_ENUM(NSInteger, UIApplicationState) {

    UIApplicationStateActive,               // Foreground 상태에서 앱이 실행되고 있을 때.   

    UIApplicationStateInactive,             // Background 등의 상태에서 Foreground 상태로 바뀔 때.

    UIApplicationStateBackground        // Background 상태에서 실행 될 때.

} NS_ENUM_AVAILABLE_IOS(4_0);


예)

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

{

    

    if (application.applicationState == UIApplicationStateActive)

    {

        // ....

    }

    else 

    {

        // ....

    }

}



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

참고

 - http://situee.blogspot.kr/2013/08/ios-handle-remote-push-notification.html   - https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/c/econst/UIApplicationStateInactive

반응형