本地通知不在iphone中触发

时间:2011-07-22 11:05:00

标签: iphone objective-c

这可能是一个重复的问题,但请帮助我。我使用下面的代码来解决本地通知,但它没有触发。我使用了一个选择器来设置触发本地通知的时间。请帮助我。

[self.actionSheet dismissWithClickedButtonIndex:0 animated:YES];
if (nil != self.data) {
     //send data picker message
     [self.target performSelector:self.action withObject:[NSNumber numberWithInt:self.selectedIndex]];
} 
else {              
     //NSDate *date = [NSDate date];
     //NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
     //[dateFormat setDateFormat:@"HH:mm:ss zzz"];
     //NSString *dateString = [dateFormat stringFromDate:date];

     //send date picker message
     NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];



     // Get the current date
     NSDate *pickerDate = [self.datePickerView date];



    //NSLog(@"date ==%@",pickerdate);
    //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]];
    //NSLog(@"day%@",[dateComponents day]);
    // 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];



    //Notification details
    localNotif.alertBody = @"Tip of the day";

    //Set the action button
    localNotif.alertAction = @"View";



    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.repeatInterval = NSDayCalendarUnit;



    localNotif.applicationIconBadgeNumber = 1;



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

 // Schedule the notification

   [localNotif release];
}

1 个答案:

答案 0 :(得分:0)

用于安排local notification

的代码在哪里

在释放UILocalNotification对象之前,您必须在某处进行此调用:

- (void)scheduleLocalNotification:(UILocalNotification *)notification

像这样,

 [[UIApplication sharedApplication] scheduleLocalNotification: localNotif];

只有在您调用此方法后才会复制notification,因此可以安全地释放通知对象。