2011-07-22 11 views
0

私のアクションシートピッカービューを使用しています。それは正常に動作しています。しかし、私は特定の時間がtime pickerに設定されているときにlocal notificationを発射したい、local notificationを発射していない。私はこのコードを使用しました:iphoneでローカル通知が発射されない

enter code here 

     - (void)actionPickerDone { 

     if (nil != self.data) { 
//send data picker message[self.target performSelector:self.action  withObject:[NSNumbernumberWithInt:self.selectedIndex]]; 
     } 
else { 
    //send date picker message 
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; 

    // Get the current date 
    NSDate *pickerDate = [self.datePicker date]; 
    // Break the date up into components 
    NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) 
                fromDate:pickerDate]; 
    NSDateComponents *timeComponents = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) 
                fromDate:pickerDate]; 

    // Set up the fire time 
    NSDateComponents *dateComps = [[NSDateComponents alloc] init]; 
    [dateComps setDay:[dateComponents day]]; 
    [dateComps setMonth:[dateComponents month]]; 
    [dateComps setYear:[dateComponents year]]; 
    [dateComps setHour:[timeComponents hour]]; 
    // Notification will fire in one minute 
    [dateComps setMinute:[timeComponents minute]]; 
    [dateComps setSecond:[timeComponents second]]; 
    NSDate *itemDate = [calendar dateFromComponents:dateComps]; 
    [dateComps release]; 

    UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 
    if (localNotif == nil) 
     return; 
    localNotif.fireDate = itemDate; 
      NSLog(@"%@what is this",itemDate); 
    localNotif.timeZone = [NSTimeZone defaultTimeZone]; 
      NSLog(@"i8i8i88i"); 

    // Notification details 
    //localNotif.alertBody = [eventText text]; 
    // Set the action button 
    localNotif.alertAction = @"View"; 

    localNotif.soundName = UILocalNotificationDefaultSoundName; 
    localNotif.applicationIconBadgeNumber = 1; 

    // Specify custom data for the notification 
    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"]; 
    localNotif.userInfo = infoDict; 

    // Schedule the notification 
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
    [localNotif release]; 
    } 

このコードには何か間違っていますか?

+0

通知がトリガーされるはずの間、アプリはフォアグラウンドになっていますか?その場合、アラートが表示されず、didRecieveLocalNotificationコールバックが発生します。 – Idan

答えて

6

私はあなたのコードを実行し、通知はありませんでした。それから私はlocalNotif.alertBody代入文のコメントを外して起動しました。 alertBodyを指定しないと、ローカル通知はアラートとして表示されません。さらに、Idanによって提起された点も有効です。あなたのアプリがフォアグラウンドにある場合、通知は表示されず、アプリケーションデリゲートにコールバックが表示されます。このコールバックでは、一貫性のためにnotification.alertBodyというメッセージでアラートを表示する必要があります。

希望はこのことができます:)あなたはいけない

PSは、youreのがやってすべてのカレンダーコンポーネントの事を必要としています。両方ともログすると、pickerDateとitemDateの両方が同じであることがわかります。

+0

[eventText text]の代わりに教えてください。あなたがそこに与えたもの。 – ishhhh

+0

私は 'localNotif.alertBody = @"ローカル通知 ";"を書いたばかりです。単に '[eventText text]'がヌルか空文字であるべきではないことを確認してください –

+0

それはうまくいきました..thanks – ishhhh

関連する問題