iPhone

[obj-c] Local Notification

tenn 2012. 12. 6. 15:06





<< AppDelegate >>


/**      Local Notificaiton      **/


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

{

    

    

    NSLog(@"didFinishLaunchingWithOptions");

    

    UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

    

    if (notification) {

    //어플리케이션이 시작될때, 노티피케이션에 의한것?

    //어플리케이션이 액티브이지 않을때, 노티피케이션에 의해 불린다면

        

        [self showAlarm:notification.alertBody];

        NSLog(@"AppDelegate didFinishLaunchingWithOptions");

        application.applicationIconBadgeNumber = 0;

    }

    

    [self.window makeKeyAndVisible];

    

    

    

    // Override point for customization after application launch.

    return YES;

}




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

    //어플리케이션이 액티브일때, 노티피케이션 수신

    

    [self showAlarm:notification.alertBody];

    application.applicationIconBadgeNumber = 0;

    NSLog(@"AppDelegate didReceiveLocalNotification %@", notification.userInfo);

    

}


- (void)showAlarm:(NSString *)text {

    

    

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alarm"

                                                        message:text delegate:nil

                                              cancelButtonTitle:@"OK"

                                              otherButtonTitles:nil];

[alertView show];

}







<< 노티피케이션을 등록할 곳 >>



    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    

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

    

    // 10초후 노티피케이션 등록

    NSDate *now = [NSDate date];

    NSDate *dateToFire = [now dateByAddingTimeInterval:5];

    

    NSLog(@"now time: %@", now);

    NSLog(@"fire time: %@", dateToFire);

    

    

    localNotification.fireDate = dateToFire;

    localNotification.alertBody = @"Time to get up!";

    localNotification.soundName = UILocalNotificationDefaultSoundName;

    localNotification.applicationIconBadgeNumber = 1; // increment

   

     NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"Object 1", @"Key 1", @"Object 2", @"Key 2", nil];

    

    localNotification.userInfo = infoDict;

    

    

     

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];