2012-01-22 11 views
2

問題は、通知が設定されている間に処理が進まないことです。ペン先に日付ピッカーとその下のボタンを置いています。ユーザーは、ピッカーに日付を設定し、ボタンをクリックすると、日付の60時間前に通知が設定されます。だから、私はちょうど日付ピッカーの日付を認識するために砲撃者を取得することに問題があるように見えます。ここでは、コードで、それは私のビューコントローラである:iOS:ローカル通知が時間通りに起動しない

- (IBAction)scheduleNotifButton:(id)sender { 
UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 

NSDate *selectedDate = [self.datePicker date]; 


localNotif.fireDate = [selectedDate dateByAddingTimeInterval:-60*60*60]; 
localNotif.timeZone = [NSTimeZone defaultTimeZone]; 


localNotif.alertBody = @"Event is in three days!"; 

localNotif.alertAction = nil; 

localNotif.soundName = UILocalNotificationDefaultSoundName; 
localNotif.applicationIconBadgeNumber = 0; 
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];  

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Event scheduled." 
               message:@"You will be notified three days before the event." 
               delegate:nil 
             cancelButtonTitle:@"Okay." 
             otherButtonTitles:nil]; 
[alert show]; 

} 

そしてここでは、いくつかの追加のコードは、それが助け場合、これはユーザーが入力しピッカーで日付を節約を扱う私のビューコントローラからより多くのコードです:

- (IBAction)dateChanged:(id)sender 
{ 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 

    NSDate *selectedDate = [self.datePicker date]; 

    [defaults setObject:selectedDate forKey:@"ImportantDatesViewController.selectedDate"]; 
    [defaults synchronize]; 

} 
- (void)viewDidLoad { 
    NSDate *storedDate = [[NSUserDefaults standardUserDefaults] 
          objectForKey:@"ImportantDatesViewController.selectedDate"]; 
    if (storedDate == nil) { 
     storedDate = [NSDate date]; 
    } 

    [self.datePicker setDate:storedDate animated:NO]; 

} 

私は今、3日間、このコードを掘ってきたし、彼らがすることになっているとき、私の通知が通過していないしている理由を理解することはできません。どんな助けでも大歓迎です、ありがとう!

答えて

3

あなたはUILocalNotificationsとリモート通知が異なることを理解していますか?

didRegisterForRemoteNotificationsWithDeviceTokenでは、リモート通知に使用するデバイストークンを取得します。サーバーからリモート通知が送信されるので、そのトークンをサーバーに保存してからサーバーを使用してリモート通知を送信する必要があります。その後、ローカル通知が分に消灯し

localNotif.fireDate = [[NSDate date] dateByAddingTimeInterval:60]; 

:これまで

localNotif.fireDate = [eventDate dateByAddingTimeInterval:-630*60]; 

は、なぜあなたはこれを変更しないでください。たぶん問題は、あなたが設定している日付です。

+0

ありがとう。アラートは送信されますが、ユーザーがピッカーに入力した日付には依存していないようです。 – John

1

通知が機能していないことをどのように知っていますか?

ローカル通知とプッシュ通知が異なります。アプリが現在アクティブな場合、ローカル通知は実際には警告メッセージを表示しません。それはちょうどUIApplicationDelegateMethodを呼び出します

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

ええ、それが原因であるはずです。とにかく、この場合にアラートを表示する場合は、手動でUIAlertを作成し、その ' - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification'メソッドの中に手動で表示できます。 – Hlung

関連する問題