2012-02-12 77 views
3

嘗試使用下面的代碼來安排UILocalNotification時碰到的問題:UILocalNotification - 通知不顯示

- (IBAction) createNotification { 
    //Just to verify button called the method 
    NSLog(@"createNotification"); 

    NSString *dateString = dateTextField.text; 

    NSString *textString = textTextField.text; 

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateFormat:@"MM-dd-yyyy HH:mm"]; 
    [formatter setTimeZone: [NSTimeZone defaultTimeZone]]; 

    NSDate *alertTime = [formatter dateFromString:dateString]; 

    UIApplication *app = [UIApplication sharedApplication]; 

    UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease]; 
    if(notification){ 
     notification.fireDate = alertTime; 
     notification.timeZone = [NSTimeZone defaultTimeZone]; 
     notification.repeatInterval = 0; 
     notification.alertBody = textString; 

     [app scheduleLocalNotification:notification]; 

     NSLog(@"%@", dateString); 
    } 
} 

代碼的調用是否正確,當我按下按鈕,我傳遞以下日期字符串:

2012年2月12日19:01

  • 我也試過

02/12/2012 19:01

無濟於事。 (相應地根據測試時間改變時間,例如21:06)

有人可以解釋爲什麼本地通知不顯示嗎?

由於提前,

傑克

+0

當你執行NSLog(@「%@」,alertTime)'時會發生什麼? – yuji 2012-02-12 20:13:47

+0

你可以嘗試以下代碼來使用presentLocalNotificationNow替換scheduleLocalNotification以確保本地通知正常工作嗎?你正在使用哪個iOS版本? – Allen 2012-02-12 20:14:35

+0

您是否嘗試過NSLog notification.fireDate以查看它顯示的時間? – Shubhank 2012-02-12 20:16:49

回答

10

本地通知交付,但不顯示(即無證件,無聲音,無警示),當應用程序正在運行,並在前臺。但是,如果您需要以某種方式對本地通知作出反應,則會調用您的應用程序委託的application:didReceiveLocalNotification:方法。

有關詳細信息,請參閱UILocalNotification class reference

+0

謝謝,但我的應用程序不調用didReceiveLocalNotification?任何想法,爲什麼這可能是?謝謝 – 2012-02-12 22:00:08

+0

所以你說你可以安排通知,如果應用程序沒有運行,你會看到通知,但是如果你按照同樣的方式安排通知並且應用程序正在運行,那麼'application:didReceiveLocalNotification'沒有被調用? – yuji 2012-02-12 22:03:27

+0

是的,這就是它。我使用這個: - (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {NSLog(@「Notification Received」); } – 2012-02-12 22:08:08