简体   繁体   English

如果数组日期与今天日期相同,则为本地通知

[英]Local notification if arrays date is same as today date

I have one array something like that: 我有一个像这样的数组:

_combinedBirthdates(
"03/05/2013",
"09/22/1986",
"03/02/1990",
"03/02",
"08/22/1989",
"11/02/1990",
"07/08",
"08/31/1990",
"05/13",
"07/11/2007",
"10/07/2010",
"02/20/1987")

i want Local notification if today date is same as date in above array 如果今天的日期与上面数组中的日期相同,我想要本地通知

I used following logic for notification: 我使用以下逻辑通知:

NSLog(@" _combinedBirthdates%@",_combinedBirthdates);  
NSDateFormatter *Formatter1 = [[NSDateFormatter alloc] init];
[Formatter1 setDateFormat:@"MM/dd"];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
NSDate *date1 =[NSDate date];
NSString *string =[Formatter1 stringFromDate:date1];
NSDate *todaydate =[Formatter1 dateFromString:string];

for (int i=0;i<_combinedBirthdates.count;i++)
{
    NSDate *date =[Formatter1 dateFromString:[_combinedBirthdates objectAtIndex:i ]];
    if(date == todaydate){
    localNotif.fireDate = date;
    localNotif.alertBody = @"birthdate notification";
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    }

Now my questions: 现在我的问题:

  1. Is this code ok? 这段代码好吗?
  2. Do I need device to test this code or I can test it in simulator? 我是否需要设备来测试此代码,或者我可以在模拟器中测试它?
  3. When notification will appear? 什么时候会出现通知? at 12:00 am? 在凌晨12:00
  4. Will notification appear if application is closed? 如果申请被关闭,是否会出现通知?
  5. If code is not ok then please modify it. 如果代码不正确,请修改它。
  1. Your code had some mistakes, I had rectified here. 你的代码有一些错误,我在这里已经纠正了。
  2. Yes, we can able to run this in simulator for testing. 是的,我们可以在模拟器中运行它进行测试。
  3. UILocalNotifications will fire on the date which we specified on the respective notification. UILocalNotifications将在我们在相应通知上指定的日期触发。 While assigning the notification, what is the time mentioned that will we taken into consideration. 在分配通知时,我们会考虑的时间是什么。 If we not set the time then the device time will the considered for the date which is mentioned for firing. 如果我们没有设定时间,那么设备时间将被考虑用于触发的日期。
  4. Even the applications is closed also, the local notification will fire, but until we tap on the action button it won't. 即使应用程序也关闭,本地通知将会触发,但在我们点击操作按钮之前它不会。 see this specification... 看这个规格......

See the modified code.... 查看修改后的代码....

NSMutableArray *newBirthDates = [[NSMutableArray alloc] init];;
for(int i = 0; i < [_combinedBirthdates count]; i++)
{
    NSString *date = [_combinedBirthdates objectAtIndex:i];
    NSArray *dateComponents = [date componentsSeparatedByString:@"/"];
    if([dateComponents count] == 3)
    {
        [newBirthDates addObject:[NSString stringWithFormat:@"%@/%@",[dateComponents objectAtIndex:0], [dateComponents objectAtIndex:1]]];
    }
    else
    {
        [newBirthDates addObject:date];
    }
}
NSDateFormatter *Formatter1 = [[NSDateFormatter alloc] init];
[Formatter1 setDateFormat:@"MM/dd"];
NSDate *date1 =[NSDate date];
NSString *string =[Formatter1 stringFromDate:date1];
NSDate *todaydate =[Formatter1 dateFromString:string];

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.alertBody = @"birthdate notification";
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM