本地通知中的Fire Date问题

时间:2012-01-31 13:06:37

标签: iphone uilocalnotification

我有一个视图控制器,其中包含一个供用户输入日期值的字段。相比之下,我有一个名为“在天之前提醒”的字段供用户选择何时应该触发通知。如果提醒前一天是同一天,然后通知设置为在日期触发,但是当提醒日在1天之前,那么通知应该在一天之前触发设置的日期(指定)。为此我写了一个名为 - (void)setNotification的方法,这里是实现代码:

- (void)setNotification
{
    //Set notification after confirmation of saved data

    Class cls = NSClassFromString(@"UILocalNotification");
    UILocalNotification *notif = [[cls alloc] init];

    if (cls != nil) 
    {
        textField = [self.fields objectAtIndex:3];

        if (textField.text == @"Same Day") 
        {
            notif.fireDate = [datePicker date];
            notif.timeZone = [NSTimeZone defaultTimeZone];
        }
        else if(textField.text == @"1 Day")
        {
            NSDate *now = [datePicker date];

            // set up date components
            NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:now];
            [components setDay:-1];

            // create a calendar to form date
            NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
            NSDate *newDate2 = [gregorian dateByAddingComponents:components toDate:now options:0];

            notif.fireDate = newDate2;
            notif.timeZone = [NSTimeZone defaultTimeZone];
        }
        notif.timeZone = [NSTimeZone defaultTimeZone];
        notif.alertBody = textView.text;
        notif.alertAction = @"View";
        notif.soundName = @"lazy_afternoon.mp3";
        notif.applicationIconBadgeNumber = 1;
        textField = [self.fields objectAtIndex:1];
        NSDictionary *userDict = [NSDictionary dictionaryWithObject:self.textField.text forKey:kReminder];
        notif.userInfo = userDict;
        [[UIApplication sharedApplication] scheduleLocalNotification:notif];
        [notif release];
    }
}

现在我们都知道,当通知被触发时,用户点击view.Then我们显示一个警告,实现代码写在appDelegate.Here它是:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{    

    // For The Purpose Of Notification.
    Class cls = NSClassFromString(@"UILocalNotification");
    if (cls) 
    {
        UILocalNotification *notification = [launchOptions objectForKey:
                                             UIApplicationLaunchOptionsLocalNotificationKey];

        if (notification) 
        {
        NSString *reminderText = [notification.userInfo objectForKey:kReminder];
        [self.viewController showReminder:reminderText];
        }
    }

    application.applicationIconBadgeNumber = 0;
}

现在收到本地通知后,我们会做以下事情,即:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
{
    application.applicationIconBadgeNumber = 0;
    NSString *reminderText = [notification.userInfo objectForKey:kReminder];
    [self.viewController showReminder:reminderText];
}

现在我已将 - (void)setNotification操作设置为右侧导航右侧栏按钮项目,标题为“保存”,如下所示:

-(IBAction)save:(id)sender
{
[self setNotification];
}

当我没有指定火灾日期的任何条件时,即简单地指定为:

notif.fireDate = [datePicker date];一切都很好,通知(没有问题)。

但是,当我按上述条件进行开火日期时,则通知不会被解雇。当我点击保存时,警报就会被触发。而当我退出模拟器时,我可以看到一些线程问题。我不明白代码有什么问题(实现)。我经历了几个链接,也是UILocalNotification的苹果documentation。根据条件,不能找出任何设置开火日期的属性或方法。

我发现了一个方法“repeatTimeInterval”,它是一个相关的,适用于必须每周,每年等重复通知的情况。这不符合“日期要求被解雇的要求。 textField中的天数为“

任何人都可以指导我,

提前感谢所有人:)

2 个答案:

答案 0 :(得分:1)

我相信如果您查看此链接,您会发现。但是从我所看到的,你提供的代码无论如何都来自这个网站。

http://useyourloaf.com/blog/2010/7/31/adding-local-notifications-with-ios-4.html

答案 1 :(得分:0)

我知道我来晚了一点,但对我来说,代码上的错误就在这里:

if (textField.text == @"Same Day")

因为您无法使用运算符==来比较NSStrings。我认为,如果您使用isEqualToString:(NSString*),它将正常工作。如果您可以编辑原始帖子并修复代码会很棒,因此寻找有关本地通知的信息的人不会遇到同样的问题。

相关问题