0

我得到了一些日期和时间,比如约会时间。我想在那个特定时间之前提醒用户。我用谷歌搜索并获得了一些知识,但仍然有点困惑,我应该在应用程序输入后台方法中在哪里编写我的代码,我必须使用警报视图或任何其他。请帮助我.

     NSString *today=[NSString stringWithFormat:@"%@ %@",appDelegate.appointmentDate,appDelegate.timeStart1];
    NSLog(@"%@",today);


    NSDateFormatter *format=[[NSDateFormatter alloc]init];

    [format setDateFormat:@"MM/dd/yyyy hh:mm a"];
    NSDate *date1=[format dateFromString:today];
           NSLog(@"%@",date1);


    NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *datefo1 =[[NSDateComponents alloc]init];
    [datefo1 setMinute:1];

  [datefo1 setSecond:0];

    NSDate *alerttime=[cal dateByAddingComponents:datefo1 toDate:date options:0];

    UIApplication* app = [UIApplication sharedApplication];
    UILocalNotification* notifyAlarm = [[UILocalNotification alloc]
                                        init];
    if (notifyAlarm)
    {
        notifyAlarm.fireDate = alerttime;
        notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
        notifyAlarm.repeatInterval = 0;

        notifyAlarm.alertBody = @"you have an appointment  in 30 minutes";
        [app scheduleLocalNotification:notifyAlarm];
    }

我想要顶部的通知我想显示一些警报,即使我的应用程序正在运行并且也没有运行。但它没有任何消息,我哪里出错了,我需要使用 nsnotification 中心吗?对不起,我的英语很差。在此先感谢

4

1 回答 1

1

您正在使用本地通知,这是一个好的开始,但您应该知道只有当您的应用程序在后台时才会显示本地通知。如果您的应用程序在时间到来时在前台运行,您将需要自己显示通知(UIView / UIAlertView)。

您可以随时安排本地通知,这实际上取决于您允许用户进行的编辑,因此,您可能需要多少取消本地通知并安排新版本。

其他几件事:

  • 考虑使用 'NSDate -dateByAddingTimeInterval' (它只是您的用例的更少代码)
  • 您要在约会中增加一分钟,因此警报将在事件发生后(并说约会在 30 分钟内?)
  • 将 NSLocalizedString 用于将显示给用户的任何字符串
  • 将来,在 appDelegate 中存储更少的数据,这不是该类的用途:)
于 2013-04-12T16:06:49.880 回答