本地通知中的火灾日期问题

发布于 2025-01-01 04:44:19 字数 3675 浏览 0 评论 0原文

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

但是,当我按照上述即触发日期的条件执行操作时,通知不会被触发。相反,当我单击“保存”时,警报会被触发。此外,当我退出模拟器时,我可以看到一些线程问题。我不知道了解代码(实现)有什么问题。我已经浏览了几个链接,还有苹果 UILocalNotification 的文档。找不到根据条件列出任何放火的属性或方法。

我发现了一种方法“repeatTimeInterval”,当必须每周、每年等重复通知时,该方法是相关且适用的。当文本字段中的提醒天数时,该方法不符合“要触发的”日期的要求是这个“

任何人都可以正确指导我吗,

先谢谢大家了:)

I have a view controller which contains a field for user to enter date value.In contrast I have a field called "Remind Before Days" for the user to select when the notification should fire.If the remind before day is same day,then notification is set to fire on the date,but when the remind day is before 1 day,then notification should fire before one day the date set(specified).For this I have written a method called -(void)setNotification and here is the implementation code:

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

Now as we are all aware that when the notification gets fired,the user clicks view.Then we show an alert,the implementation code is written in appDelegate.Here it is:

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

Now after local notification is received,we do the following i.e.:

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

Now I have set the -(void)setNotification action to right navigation right bar button item titled "Save" as follows:

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

When I don't specify any condition for fire date i.e. simply assign as :

notif.fireDate = [datePicker date]; everything's fine with notification(no issues).

But when I do as the above i.e. condition for fire date,then the notification is not getting fired.Instead the alert is getting fired when I click save.Also when I quit the simulator,I could see some thread problem.I don't understand what's wrong with the code (implementation).I have gone through several links,also the apple documentation of UILocalNotification.Couldn't find out any property or method to set fire Date according to conditions.

I found out a method "repeatTimeInterval" which is relavant and applicable when a notification must be repeated weekly,yearly etc..which doesn't suit the requirement that the "date to be fired is this when the remind days in textField is this"

Can any one please guide me right,

Thanks all in advance :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剩余の解释 2025-01-08 04:44:19

我相信如果你检查这个链接你就会知道。但据我所知,您提供的代码无论如何都是来自该网站的。

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

I believe if you check this link out you will find out. But from what I have seen the code you have provide is from this site anyway.

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

×眷恋的温暖 2025-01-08 04:44:19

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

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

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

I know I arrived a little late, but for me, the error on your code is here:

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

Because you can't compare NSStrings using the operator ==. I think that if you use isEqualToString:(NSString*) it will work properly. It would be great if you could edit your original post and fix the code, so people looking for info about local notifications won't run into the same problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文