简体   繁体   English

本地通知未在 iphone 中触发

[英]Local notification not firing in iphone

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 .但是我想在time picker中设置特定时间时触发本地local notification ,它不会触发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.然后我取消了 localNotif.alertBody 赋值语句的注释,它确实触发了。 Local notification will not appear as an alert if you do not specify an alertBody.如果您未指定 alertBody,本地通知将不会显示为警报。 Plus the point raised by Idan is also valid.加上 Idan 提出的观点也是有效的。 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.如果您的应用程序在前台,则不会显示通知,而是您将在应用程序委托中获得回调,您应该在其中显示带有消息 notification.alertBody 的警报以保持一致性。

Hope this helps:)希望这可以帮助:)

PS you dont need all the calender components thing youre doing. PS你不需要你正在做的所有压延机组件。 Log both of them and youll see that both pickerDate and itemDate are same.记录它们,你会发现pickerDate 和itemDate 是一样的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM