티스토리 뷰

Mobile/iOS

[iOS]LocalNotification

out of coding 2016. 3. 10. 17:25

LocalNotification을 사용하는 방법.


* UIResponder에서 정의


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

    [self registerForRemoteNotification];

    

    UILocalNotification* localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

    if(localNotification) {

        application.applicationIconBadgeNumber = 0;

    }

    

    return YES;

}


- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {

    UIApplicationState state = [application applicationState];

    if(state == UIApplicationStateActive) {

        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Reminder" message:notification.alertBody delegate:self cancelButtonTitle:@"확인" otherButtonTitles:nil, nil];

        [alert show];

    }

    

    [[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadData" object:self];

    

    application.applicationIconBadgeNumber = 0;

}


- (void)registerForRemoteNotification {

    if ([[[UIDevice currentDevice] systemVersion] floatValue] > 8.0) {

        UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert;

        UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];

        [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];

    } else {

        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

    }

}


#ifdef __IPHONE_8_0

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {

    [application registerForRemoteNotifications];

}

#endif


* Active한 상황에서 다음과 같이 등록.


UILocalNotification* localNotification = [[UILocalNotification alloc] init];

localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];

localNotification.alertBody = @"Hello world!";

localNotification.timeZone = [NSTimeZone defaultTimeZone];

localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

[[NSNotificationCenter defaultCenter] postNotificationName:@"LocalNotification" object:self];

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

'Mobile > iOS' 카테고리의 다른 글

[iOS]ViewController의 Life Cycle delegate  (0) 2016.03.17
[iOS]Volume 조절  (0) 2016.03.17
[iOS]Deprecation Warning 없애기  (0) 2016.03.10
[iOS]Storyboard 호출하기  (0) 2016.03.10
[iOS]NSMutableURLRequest 사용  (0) 2016.03.09
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함