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 pm,然后我等待 1 分钟,它变成了 18/3,但我没有收到任何通知时间我的模拟器总是和我的电脑时间一样。

4

1 回答 1

0

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

让你做一个简单的检查!

NSDateFormatter *Formatter1 = [[NSDateFormatter alloc]init];
Formatter1.dateFormat =@"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-03-18 12:15:45 +0000
格式化日期:2000-03-17 18:30:00 +0000

那你将如何获得通知?

于 2013-03-18T12:19:49.300 回答