2013-03-18 63 views
1

我用下面的代码为本地通知本地通知问题

for (int i=0;i<newBirthDates.count;i++) 
{ 
    NSDate *date =[Formatter1 dateFromString:[newBirthDates objectAtIndex:i ]]; 
    NSComparisonResult result = [date compare:todaydate]; 
    if(result == NSOrderedSame) 
    { 
     UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 
     localNotif.fireDate = date; 
     localNotif.timeZone = [NSTimeZone defaultTimeZone]; 
     localNotif.alertBody = @"birthdate notification"; 
     localNotif.alertAction = @"View"; 

     localNotif.soundName = UILocalNotificationDefaultSoundName; 
     localNotif.applicationIconBadgeNumber = 1; 
     [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
    } 
} 
    newBirthdate is array with dates in it 
    Formatter1 =MM/dd. 

让说在newbrithdate日期之一是18/3所以我要做的就是我
日期更改我的电脑和17/3 11时59分,然后我等待1分钟,它变成18/3,但我没有得到任何通知 时间在我的模拟器总是与我的电脑时间相同。

+0

您是否尝试首先打印日期? – nsgulliver 2013-03-18 11:13:03

+0

是的日期是在该数组中的MM/dd合成 – user1660882 2013-03-18 11:14:33

+0

如果你知道你的timeZone,那么为什么不设置你自己的时区,看它是否有效? – nsgulliver 2013-03-18 11:18:31

回答

0

您正在将格式化的日期字符串存储在数组中(以MM/dd格式)并将其转换回日期以设置通知。

让我们来简单检查一下!

NSDateFormatter *Formatter1 = [[NSDateFormatter alloc]init]; 
Formatter1.dateFormat [email protected]"MM/dd"; 

NSDate *today = [NSDate date];// take current date 
NSString *initialDateString =[Formatter1 stringFromDate:today]; // formatted date string(in MM/dd) 


NSDate *dateFromFormatter = [Formatter1 dateFromString:initialDateString]; // Date from the formatted string(In your case taking from array) 
NSString *finalDateString = [Formatter1 stringFromDate:dateFromFormatter];// formatted string from new date 

// lets take a look wether both are same 
NSLog(@"Initial date string: %@",initialDateString); 
NSLog(@"Final date string : %@",finalDateString); 
// Both date strings are same 

NSLog(@"Actual date : %@",today); 
NSLog(@"Formatted date : %@",dateFromFormatter); 
//But see both dates are entirely different 

输出

初始日期字符串:03/18
截止日期字符串:03/18
实际日期:2013年3月18日12点15分45秒0000
格式化日期:2000-03-17 18:30:00 +0000

那你怎么会得到通知?

+0

所以什么是解决方案客户日期将总是传出用户正在睡觉,然后应用程序已关闭,然后是上午12点,然后日期得到改变,然后它的日期是相同的,所以如何我会得到通知的时间? – user1660882 2013-03-18 13:30:46

+0

保存日期(日期对象不是字符串)。同时比较两个日期所需的组件(月和日)。最后设定通知。请记住通知是特定时间的,因此请相应更正日期。更改实施。有任何疑问让我知道...会帮助你 – 2013-03-18 14:06:30