Skip to content

Instantly share code, notes, and snippets.

@dittos
Created June 20, 2013 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dittos/5821349 to your computer and use it in GitHub Desktop.
Save dittos/5821349 to your computer and use it in GitHub Desktop.
push notification handling boilerplate
@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