Created
June 20, 2013 09:08
-
-
Save dittos/5821349 to your computer and use it in GitHub Desktop.
push notification handling boilerplate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface AppDelegate () { | |
NSDictionary *pushUserInfo; | |
} | |
@end | |
@implementation AppDelegate | |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { | |
// ... | |
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound]; | |
NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; | |
if (userInfo) | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
[self handlePush:userInfo]; | |
}); | |
return YES; | |
} | |
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { | |
// FIXME: USE YOUR CODE HERE | |
[[PushTokenManager sharedManager] sendToken:deviceToken]; | |
} | |
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { | |
NSLog(@"Failed to register remote notification: %@", error); | |
} | |
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { | |
if (application.applicationState == UIApplicationStateInactive) { | |
// 푸시를 눌러서 앱을 실행했음 | |
[self handlePush:userInfo]; | |
} else { | |
// 앱이 실행된 상태에서 푸시를 수신함 | |
pushUserInfo = userInfo; | |
NSString *msg; | |
id payload = [userInfo valueForKeyPath:@"aps.alert"]; | |
if ([payload isKindOfClass:[NSString class]]) | |
msg = payload; | |
else | |
msg = payload[@"body"]; | |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil | |
message:msg | |
delegate:self | |
cancelButtonTitle:@"취소" | |
otherButtonTitles:@"자세히 보기", nil]; | |
[alert show]; | |
} | |
} | |
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex { | |
if (buttonIndex == 1) { | |
// "자세히 보기"를 눌렀을 경우 | |
[self handlePush:pushUserInfo]; | |
} | |
pushUserInfo = nil; | |
} | |
- (void)handlePush:(NSDictionary *)userInfo { | |
[self clearNotifications]; | |
// FIXME: TAKE ACTION! | |
} | |
- (void)clearNotifications { | |
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1]; | |
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment