开发者

Local notification not firing in iphone

开发者 https://www.devze.com 2023-03-21 23:43 出处:网络
In my i\'m using a action sheet picker view. It is working fine. But I want to fire a local notification when a particular time is set in time picker, it is not firing the local notification. I used t

In my i'm using a action sheet picker view. It is working fine. But I want to fire a local notification when a particular time is set in time picker, it is not firing the local notification. I used this code:

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

Is there any thing wrong in this code.please help me.


I executed your code and the notification didnt fire. Then I uncommented the localNotif.alertBody assignment statement and it did fire. Local notification will not appear as an alert if you do not specify an alertBody. Plus the point raised by Idan is also valid. If your app is in foreground the notif wont be displayed rather you will get a callback in your app delegate in which you should display an alert with the message notification.alertBody for consistency.

Hope this helps :)

PS you dont need all the calender components thing youre doing. Log both of them and youll see that both pickerDate and itemDate are same.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号