Локальное уведомление не срабатывает в iphone - PullRequest
0 голосов
/ 22 июля 2011

По моему я использую вид выбора листа действий. Работает нормально. Но я хочу запустить local notification, когда определенное время установлено в time picker, оно не запускает local notification. Я использовал этот код:

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];
    }

Что-то не так в этом коде? Пожалуйста, помогите мне.

1 Ответ

6 голосов
/ 22 июля 2011

Я выполнил ваш код, и уведомление не сработало. Затем я раскомментировал оператор присваивания localNotif.alertBody, и он сработал. Локальное уведомление не будет отображаться как предупреждение, если вы не укажете alertBody. Плюс пункт, поднятый Иданом, также действителен. Если ваше приложение находится на переднем плане, уведомление не будет отображаться, скорее вы получите обратный вызов в делегате приложения, в котором вы должны отобразить предупреждение с сообщением уведомлением.alertBody для согласованности.

Надеюсь, это поможет:)

PS Вам не нужны все компоненты каландра, которые вы делаете. Зарегистрируйте их оба, и вы увидите, что и pickerDate, и itemDate одинаковы.

...