2011-02-16 8 views
0

私は通知をスケジュールし、それが警告メッセージを表示する必要がある前に警告60分にそれを与える...UILocalNotificationスケジュールアラート

私は通知を追加するとすぐに私のアプリのデリゲートのメソッドが呼び出された

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

アラートとしてバックグラウンドに表示されることがわかりますか?私はオーバーライドしたり、それが60分間隔でスケジュール警告を与えられたことを確認するために使用する必要がある任意の他のデリゲートメソッドが...あり

- (void)scheduleNotificationWithItem:(NSDate *)item interval:(int)minutesBefore 
{ 
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; 

    NSDateComponents *dateComps = [[NSDateComponents alloc] init]; 
    //NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 

    NSDateComponents *currentDateComponents = [calendar components:(NSWeekdayCalendarUnit | 
                    NSYearCalendarUnit | NSMonthCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSWeekCalendarUnit | NSMinuteCalendarUnit) fromDate:item]; 

    NSLog(@"- current components year = %i , month = %i , week = % i, weekday = %i", [currentDateComponents year], [currentDateComponents month], [currentDateComponents week], [currentDateComponents weekday]); 

    NSLog(@"[currentDateComponents minute]: %i", [currentDateComponents minute]); 
    NSLog(@"[currentDateComponents hour]: %i",  [currentDateComponents hour]); 
    NSLog(@"[currentDateComponents day]: %i",  [currentDateComponents day]); 
    NSLog(@"[currentDateComponents week]: %i",  [currentDateComponents week]); 
    NSLog(@"[currentDateComponents month]: %i",  [currentDateComponents month]); 
    NSLog(@"[currentDateComponents year]: %i",  [currentDateComponents year]); 

    [dateComps setDay: [currentDateComponents day]]; 

    [dateComps setMonth:[currentDateComponents month]]; 

    [dateComps setYear:[currentDateComponents year]]; 

    [dateComps setHour:[currentDateComponents hour]]; 

    [dateComps setMinute:[currentDateComponents minute]]; 

    NSDate *itemDate = [calendar dateFromComponents:dateComps]; 

    [dateComps release]; 

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

    if (localNotif == nil) 
     return; 

    localNotif.fireDate = [itemDate addTimeInterval:-(minutesBefore*60)]; 

    localNotif.timeZone = [NSTimeZone defaultTimeZone]; 

    localNotif.alertBody = [NSString stringWithFormat:@"%@\n%@", 
          streetAddress, 
          stringOfWhenAuctionIsOn]; 

    localNotif.alertAction = NSLocalizedString(@"View Details", nil); 

    localNotif.soundName = UILocalNotificationDefaultSoundName; 

    localNotif.applicationIconBadgeNumber = 1; 

    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:streetAddress 
                 forKey:idOfStreetAlert]; 

    localNotif.userInfo = infoDict; 

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 

答えて

1

通知がすぐに表示される場合は、fireDateプロパティに問題がある可能性があります。送信された日付が正しいことを確認しようとしているようですが、fireDateが期待どおりのものであることも確認する必要があります。

また、あなたはおそらく同じ効果を達成するために

localNotif.fireDate = [item dateByAddingTimeInterval:-60*minutesBefore]; 

を行うことができ、NSDateComponentsの周り混乱する必要はありません。私はこれをテストしていないが、うまくいくかもしれない。

タイマーが起動してアプリが実行されていないときはどうなりますか。アプリが終了していない場合はapplication:didReceiveLocalNotification:が呼び出されます。反対側にあなたのアプリが終了している場合は、を@Ishuが指摘してチェックする必要があります。私は通常、コードの重複を避けるために通知を一般的な方法に渡します。

+0

ちょうど不思議なことに、なぜ間隔を負の値に設定していますか?\ –

0

、あなたがdidFinishLaunchingWithOptionsでコードを記述する必要が背景ではありませんデリゲートメソッド、

UILocalNotification *localNotif = 
    [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; 

    if (localNotif) { 
     //your code 
     NSLog(@"Recieved Notification %@",localNotif); 
    } 

アプリが背景から通知を受け取ったときにロジックを作成します。通知アプリが通知を受け取ることを心配しないでください。

関連する問題