2012-01-29 12 views
0

私は特定のユーザの選択した日にローカル通知を発生させる目覚まし時計アプリケーションを作っています。つまり、ユーザがM、F、土を選択した場合、M/F /のみ。私が持っている問題は、私の血まみれの警報がその日に毎日発砲しているということです。どのように私はそれをユーザーが選択した日に制限できますか?最も近い月曜日に1つ、最も近い水曜日と金曜日に最も近いとそれらのそれぞれにnotification.repeatInterval = NSWeekCalendarUnitを設定します。ここに私のコード特定の日にUILocalNotificationを繰り返す方法

//For testing purposes I want the alarm to fire only on Monday at 7:00am. 
//This fires every day at 7:00am 
[[UIApplication sharedApplication] cancelAllLocalNotifications]; 

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 

NSDate *now = [NSDate date];  

    // set components for time 7:00 a.m. Monday 
    NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: now]; 

    [componentsForFireDate setWeekday: 2] ; 
    [componentsForFireDate setHour: 7] ; 
    [componentsForFireDate setMinute:0] ; 
    [componentsForFireDate setSecond:0] ; 

    NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate]; 

    // Create the notification 
    UILocalNotification *notification = [[UILocalNotification alloc] init] ; 

    notification.fireDate = fireDateOfNotification ; 
    notification.timeZone = [NSTimeZone localTimeZone] ; 
    notification.alertBody = [NSString stringWithFormat: @"Wake up!"] ; 
    notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Waking Up time"] forKey:@"wakeUp"]; 

//Problem is here NSDayCalendarUnit makes my alarm fire every day 
    notification.repeatInterval= NSDayCalendarUnit ; 

    //notification.soundName=UILocalNotificationDefaultSoundName; 
    [email protected]"alarm-clock-ringing-01.wav"; 

    [[UIApplication sharedApplication] scheduleLocalNotification:notification] ; 
+0

何らかの理由で私の日付が間違っています。つまり、NSLogを今すぐ行っているときです:2012-01-29 22:00:37 +0000 fireDateOfNotification:2012-01-01 21:58:00 +0000 –

+0

あなたの問題を解決しましたか? – chancyWu

答えて

0

次の3つのアラームを作ることができます。

+0

私はNSWeekCalendarUnitを試しましたが、毎日アラームを鳴らします。つまり、日曜日 –

1

実際にローカル通知を1回だけ発生させても、デバイスが通知を受信した場合は、同じ通知を再度通知するようにしてください。これが機能するには、アプリケーションを実行する必要はありません。

アプリケーションがアプリデリゲートのapplicationDidFinishLaunchingでは、実行されていないときは、ローカル通知を扱う:

UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; 

// handles notification when application is relaunched after being terminated 
// not when app is already in the foreground running. 
if(localNotif) 
{ 
    // repeat the same local notification again 
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
} 

注:repeatIntervalはあなたが(この場合は「日指定単位で同じ操作を実行しますがあなたが通知日として指定したものに関係なく毎日繰り返されます)

関連する問題