iPhone不会触发本地通知 [英] Local notification not firing in iphone

查看:75
本文介绍了iPhone不会触发本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我中,我正在使用操作表选择器视图.一切正常.但是我想在time picker中设置特定时间时触发local notification,它不是触发local notification.我使用了以下代码:

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.

推荐答案

我执行了您的代码,通知没有触发.然后,我取消注释localNotif.alertBody赋值语句,并且确实触发了.如果您未指定alertBody,则本地通知将不会显示为警报.再加上伊丹提出的观点也是有效的.如果您的应用程序位于前台,则不会显示该通知,而您会在应用程序委托中收到回调,在其中应显示一条警报,并显示消息notification.alertBody以确保一致性.

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.

希望这会有所帮助:)

PS,您不需要您做的所有压延组件.记录它们两者,您将看到pickerDate和itemDate都相同.

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.

这篇关于iPhone不会触发本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆