在我的我使用的是一個操作選單視圖。它工作正常。但是我想在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];
}
這段代碼有什麼不對嗎?請幫助我。
當通知應該被觸發時,您的應用程序是否處於前臺?如果是這樣,則不會看到提醒,只會觸發didRecieveLocalNotification回調。 – Idan