【问题标题】:Fire Date issue in local notification本地通知中的火灾日期问题
【发布时间】:2012-01-31 13:06:36
【问题描述】:

我有一个视图控制器,其中包含一个供用户输入日期值的字段。相比之下,我有一个名为“提醒前几天”的字段供用户选择何时触发通知。如果前一天的提醒是同一天,然后通知设置为在该日期触发,但是当提醒日在 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];
    }
}

现在我们都知道,当通知被触发时,用户点击视图。然后我们显示一个警报,实现代码写在 appDelegate 中。这里是:

- (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 日期];通知一切正常(没有问题)。

但是当我按照上述操作时,即触发日期的条件,则不会触发通知。相反,当我单击保存时会触发警报。此外,当我退出模拟器时,我可能会看到一些线程问题。我不明白代码(实现)有什么问题。我浏览了几个链接,也是 UILocalNotification 的苹果documentation。找不到任何属性或方法来根据条件设置触发日期。

我发现了一种方法“repeatTimeInterval”,当通知必须每周、每年等重复一次时,该方法是相关和适用的textField 中的天数是这样的”

谁能指导我一下,

提前谢谢大家:)

【问题讨论】:

  • 有人觉得这个帖子有点长吗??
  • @Sarah 对不起,我想具体一点,以便专家可以更好地理解问题并轻松回答。您能帮帮我吗,谢谢 :)
  • 您能否简要解释一下,因为由于时间较短,我无法阅读全文。
  • 哦,当然,我有一个文本字段可以在几天前选择提醒。如果提醒日是同一天,那么通知触发日期将是 [datePicker date];当它是 1 天时,应该触发通知在从 datePicker 选择的 1 天之前。但是通知没有触发,而是显示警报,当我们单击通知的查看按钮时应该显示该警报,当我退出模拟器时,出现线程问题,这就是问题,谢谢 :)
  • 我需要赶时间。仍然会告诉您在代码中放置断点并尝试调试,您​​肯定会遇到错误。对不起..

标签: iphone uilocalnotification


【解决方案1】:

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

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

【讨论】:

  • 感谢马特的回答,但是在代码中您可以观察到没有特定条件,例如 if(textField.text == ...) notif.fireDate = [datePicker date],something像这样,它只是[datePicker date]。即使在我的情况下也适用。但是正如你在我的代码sn-p中看到的那样,我已经指定了开火日期的条件。然后这个问题出现了,所以请帮助我,谢谢提前:)
  • 实际上,当我解决了将文本字段正确添加到数组的问题时,一切都解决了
【解决方案2】:

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

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

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

【讨论】:

  • 我遇到了这个问题,因为我犯了与这个答案类似的错误 - 我比较了这样的通知的触发日期notif.fireDate > someDate,而实际上你应该比较像这样的问题的日期:@987654321 @
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多