ホームボタンで終了後に通知する

2012/05/28

目覚ましのように定期時間ではなく、ホームボタンを押した後に定期時間に通知させる場合、applicationDidEnterBackground にLocalNotification処理を記述する。

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [self addNotification];
}

- (void)cancelNotification {
    [[UIApplication sharedApplication] cancelAllLocalNotifications];  
}

- (void)addNotification {
    [self cancelNotification];
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    BOOL isNotification = [defaults boolForKey:SETTING_IS_NOTIFICATION];
    if (!isNotification) return;
    
    int timeIndex = [defaults integerForKey:SETTING_NOTIFICATION_TIME];
    int sec = 60;
    NSDate  *today = [NSDate date];
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.fireDate = [today dateByAddingTimeInterval:sec];
    notification.timeZone = [NSTimeZone defaultTimeZone];
    notification.alertBody = [NSString stringWithString:NSLocalizedString(@"MESSAGE_NOTIFICATION", nil)];
    notification.alertAction = NSLocalizedString(@"Open", nil);
    notification.soundName = UILocalNotificationDefaultSoundName;
    //localNotif.applicationIconBadgeNumber = localNotif.applicationIconBadgeNumber + 1;
    //NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"local notify" forKey:@"Key"];
    //notification.userInfo = infoDict;
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}