2012-09-05 18 views
2

当我从dateTimePicker.date获取时间/日期时,通知工作,但是当我用dateFromString激发日期时,通知不起作用。scheduleLocalNotification不起作用

的代码:

-(IBAction)alarmSetButtonTapped:(id)sender { 

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
dateFormatter.locale = [NSLocale systemLocale]; 
dateFormatter.timeStyle = NSDateFormatterShortStyle; 
dateFormatter.dateStyle = NSDateFormatterShortStyle; 

NSString *tmp = @"2012-09-05 10:19"; 
[dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm"]; 
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; 

NSDate *dateFromString = [dateFormatter dateFromString: tmp]; 
NSLog(@"Alarm TMP %@", dateFromString); 
[dateFormatter release]; 


[self scheduleLocalNotificationWithDate:dateFromString]; 
[self presentMessage:@"Alarm set"]; 

};

请你帮帮我吗?

编辑:

-(void) scheduleLocalNotificationWithDate:(NSDate *)fireDate { 

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

notification.fireDate = fireDate; 
notification.alertBody = @"Time to wake up"; 
notification.soundName = @"clock.mp3"; 

[[UIApplication sharedApplication] scheduleLocalNotification:notification]; 

[notification release]; 
} 
+0

你可以张贴'scheduleLocalNotificationWithDate'代码? – janusbalatbat

+0

'NSLog(@“Alarm TMP%@”,dateFromString);'? – jjv360

回答

7
UILocalNotification *localNotification = [[UILocalNotification alloc] init]; 

    // Set the fire date/time 
    [localNotification setFireDate:yourDate]; 
    [localNotification setTimeZone:[NSTimeZone defaultTimeZone]]; 

    localNotification.applicationIconBadgeNumber=1; 

    // Setup alert notification 
    [localNotification setAlertAction:@"Open App"]; 
    [localNotification setAlertBody:[randonQuotesDic objectForKey:@"Rajneesh"]]; 
    [localNotification setAlertBody:@"You had set a Local Notification on this time"]; 

    localNotification.soundName=UILocalNotificationDefaultSoundName; 
    [localNotification setHasAction:YES];  
    UIApplication *app=[UIApplication sharedApplication]; 
    [app scheduleLocalNotification:localNotification];