[英]Fire Date issue in local notification
我有一个视图控制器,其中包含一个供用户输入日期值的字段。相比之下,我有一个名为“几天前提醒”的字段,供用户选择何时触发通知。如果前一天的提醒是同一天,则通知设置为在日期触发,但当提醒日为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的apple 文档 。找不到根据条件设置触发日期的任何属性或方法。
我发现了一种“ repeatTimeInterval”方法,该方法很适合并且适用于必须每周,每年等重复发送通知的情况。这不符合“ textField中的提醒天数为今天时要触发的日期为这一天”的要求
谁能指导我,
提前谢谢大家:)
我相信,如果您查看此链接,将会找到答案。 但是从我所看到的来看,您提供的代码还是来自此站点。
http://useyourloaf.com/blog/2010/7/31/adding-local-notifications-with-ios-4.html
我知道我来晚了一点,但是对我来说,您代码上的错误在这里:
if (textField.text == @"Same Day")
因为您不能使用运算符==
比较NSString。 我认为,如果您使用isEqualToString:(NSString*)
,它将正常工作。 如果您可以编辑原始帖子并修复代码,那就太好了,这样,人们在寻找有关本地通知的信息时就不会遇到同样的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.