본문 바로가기

iPhone & Cocoa

iOS App - Push Message 전송하기 (APNS)

728x90
반응형

iOS App에 푸쉬메시지를 전송하기 위해서는 앱이 실행될 때 해당 기기를 푸쉬메시지 서버에 등록해야 하는 과정이 필요하다.


DevTongFramework 를 사용하는 경우라면 다음과 같은 코드로 DevTong 사이트에 기기를 등록시킬 수 있다.


아래의 코드를 AppDelegate.m 파일에 작성해준다.


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// APNSManager
#import <DevTongFramework/DevTongAPNSManager.h>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // APNSManager
    NSLog(@"Registering for APNS(Apple Push Notification Service)...");
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationType)(UIRemoteNotificationTypeAlert
                                                                                                     | UIRemoteNotificationTypeBadge
                                                                                                     | UIRemoteNotificationTypeSound
                                                                                                     )];
    // Badge 개수 설정
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    // 서버로 모든 푸쉬메시지에 대한 읽음 표시를 올린다.
    [[DevTongAPNSManager sharedManager] resetcheckAllMessages];
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


    return YES;
}


위 코드는 애플 서버에 푸쉬메시지 대상기기로 등록하는 과정이다. registerForRemoteNotificationTypes 메소드 실행 후 애플서버로부터 성공 또는 실패에 대한 메시지를 받게 되는데 성공 메시지를 받았을 때 DevTong 서버에 푸쉬메시지 대상기기로 등록하는 과정을 다시한번 수행해주면 된다.

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// APNSManager

/**
 * Fetch and Format Device Token and Register Important Information to Remote Server
 */
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken
{
#if !TARGET_IPHONE_SIMULATOR
   
    BOOL sandbox = YES;
#ifdef _APPSTORE
    sandbox = NO;
#endif
   
    [[DevTongAPNSManager sharedManager] registerDevice:devToken sandbox:sandbox];
   
#endif
}

/**
 * Failed to Register for Remote Notifications
 */
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
#if !TARGET_IPHONE_SIMULATOR
   
    NSLog(@"Error in registration. Error: %@", error);
   
#endif
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

이제 앱이 푸쉬메시지를 전달받았을 때 그 내용을 표시해주기 위한 코드를 작성해주기만 하면 된다.


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * Remote Notification Received while application was open.
 */
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
#if !TARGET_IPHONE_SIMULATOR
   
    NSLog(@"remote notification: %@",[userInfo description]);
    NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
   
    NSString *alert = [apsInfo objectForKey:@"alert"];
    NSLog(@"Received Push Alert: %@", alert);
   
    NSString *sound = [apsInfo objectForKey:@"sound"];
    NSLog(@"Received Push Sound: %@", sound);
   
    NSString *badge = [apsInfo objectForKey:@"badge"];
    NSLog(@"Received Push Badge: %@", badge);
   
    NSString* messageid = [userInfo objectForKey:@"messageid"];
    NSLog(@"push message ID = [%@]", messageid);
   
    if (alert)
    {
        UIAlertView* alertView = [[[UIAlertView alloc] initWithTitle:@"Push Message" message:alert delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil] autorelease];
        [alertView show];
    }
   
    if (sound)
    {
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//#import <AudioToolbox/AudioToolbox.h>
    }
   
    // Badge 개수 설정
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    // 서버로 모든 푸쉬메시지에 대한 읽음 표시를 올린다.
    [[DevTongAPNSManager sharedManager] resetcheckAllMessages];

   
#endif
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


위 코드에서 resetcheckAllMessages 메소드는 앱에 전달된 푸쉬메시지가 모두 읽음처리되었음을 서버에게 알려주기 위한 메소드이다.


이제 devtong.com 에 푸시메시지 전송을 위한 인증서를 등록하는 과정을 설명하려고 한다.


우선 Apple 개발자 포털 사이트에서 App IDs에서 해당 앱의 App ID 가 Push Notification 이 가능하도록 설정되어 있는지를 확인해야 한다.




만약 위 그림에 표시되어 있는 부분이 Configurable 로 표시되고 있다면 오른쪽의 Configure 링크를 눌러서 단계에 따라 활성화를 시켜주면 된다.


Development, Production 두 가지 종류의 Push Notification 용 인증서를 다운로드받아서 devtong.com 사이트의 Download 메뉴의 How To Make PEM 문서파일에 설명하고 있는 과정대로 pem 파일을 작성한다.


로컬에 작성된 pem 파일을 devtong.com 사이트의 Manage - Bundle ID 메뉴에서 등록시켜준다.


특정 기기에 대해서 Push Notification 을 테스트하기 위해서 devtong.com 사이트의 Notify - Devices 메뉴에서 해당 기기의 User ID 를 등록시켜준다. 기기의 User ID 는 애플 앱스토어에서 배포되고 있는 개발통 앱을 설치하여 로그인하면 쉽게 등록시킬 수 있다.


이제 Notify - Push Message 메뉴에서 푸시메시지를 전송해볼 수가 있다.

반응형

'iPhone & Cocoa' 카테고리의 다른 글

iOS App - 공지사항 전달  (0) 2012.07.04
iOS App - InApp 영수증정보 검증  (0) 2012.07.04
iOS App - Crash Log 파일 전송하기  (0) 2012.07.04
iOS App - Crash Log 작성하기  (0) 2012.07.04
iOS App - AdHoc 버전 배포하기  (0) 2012.07.04