本地通知中的问题没有解雇

时间:2013-07-22 11:12:31

标签: ios uilocalnotification

下面是我的代码我在互联网上检查一切似乎没问题但不知道通知是不是解雇我知道它是如此简单但我仍然卡在这里请帮助

    -(void)notification:(NSMutableArray *)nameArray datearray:(NSMutableArray*)datearray;

    {
        NSLog(@"name%@",nameArray);
        NSLog(@"date%@",datearray);

         "Jodie Hession",
    "John Miller",

    "Shane Jarome",
    "Sharon Scott",
    "Sheron Begley",
    "Susan Diaz",
    Kate,
    John,
    Anna,
    David,
    today,
    "After Some day"
)
date(
    "14/12/2013 04:35:00",
    "18/12/2013 04:35:00",
    "04/04/2014 04:35:00",
    "01/01/2014 04:35:00",
    "29/06/2014 04:35:00",
    "05/01/2014 04:35:00",
    "18/12/2013 04:35:00",
    "20/01/2014 04:35:00",
    "22/06/2014 04:35:00",
    "29/08/2013 04:35:00",
    "15/06/2014 04:35:00",
    "22/07/2013 04:35:00",
    "31/07/2013 04:35:00"
)


    // logic for local notification start
    [[UIApplication sharedApplication] cancelAllLocalNotifications];




    NSDateFormatter *Form=[[[NSDateFormatter alloc]init]autorelease];
    [Form setDateFormat:@"dd/MM/yyyy hh:mm:ss"];


    UILocalNotification *notification = [[UILocalNotification alloc] init];
    for (int i=0;i<datearray.count;i++)
    {
        NSDate *date =[Form dateFromString:[datearray objectAtIndex:i ]];

        NSLog(@"%@",date);

        if(notification)
        {

            notification.fireDate = date;

            //            if(notification.fireDate == date){
            //
            //                notification.applicationIconBadgeNumber = 1;
            //
            //            }


            notification.timeZone = [NSTimeZone defaultTimeZone];
            notification.alertBody = [NSString stringWithFormat:@"Today is %@\'s Birthday",[nameArray objectAtIndex:i]];
            notification.alertAction = @"View";

            notification.soundName = UILocalNotificationDefaultSoundName;

            [[UIApplication sharedApplication] scheduleLocalNotification:notification];
        }

        NSLog(@"fire date%@",  notification.fireDate);
     //  fire date2013-07-30 23:05:00 +0000

不知道为什么但是如果我们用stringfromdate检查它,那么nsdate总会打印其他内容然后只显示正确的值         }

}

并在我的app delegete

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{


    NSLog(@"Notification Received, %@, set for date %@", notification.alertBody, notification.fireDate);
}

1 个答案:

答案 0 :(得分:1)

看起来这是您正在使用的日期格式的问题。我使用GMTsetDateFormat:@"yyyy-MM-dd HH:mm:ss Z"中创建了它,现在工作正常。

以下是您的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   NSMutableArray *datearray = [[NSMutableArray alloc]initWithObjects:
                             @"23/07/2013 12:33:00",
                             @"23/07/2013 12:33:05",
                             @"23/07/2013 12:33:10",
                             @"23/07/2013 12:33:15",
                             @"23/07/2013 12:33:20",
                             @"23/07/2013 12:33:25",
                             @"23/07/2013 12:33:30",
                             @"23/07/2013 12:33:35",
                             @"23/07/2013 12:33:40",
                             @"23/07/2013 12:33:45",
                             @"23/07/2013 12:33:50",
                             @"23/07/2013 12:33:55",
                             @"23/07/2013 12:34:00",
                             nil];
    NSMutableArray *nameArray = [[NSMutableArray alloc]initWithObjects:
                                 @"Jodie Hession",
                                 @"John Miller",
                                 @"Shailesh Mistry",
                                 @"Shane Jarome",
                                 @"Sharon Scott",
                                 @"Sheron Begley",
                                 @"Susan Diaz",
                                 @"Kate",
                                 @"John",
                                 @"Anna",
                                 @"David",
                                 @"today",
                                 @"After Some day",
                                 nil];
    [self notification:nameArray datearray:datearray];
}

-(void)notification:(NSMutableArray *)nameArray datearray:(NSMutableArray*)datearray;
{
    NSDateFormatter *Form = [[NSDateFormatter alloc]init];
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    for (int i=0;i<datearray.count;i++)
    {
        //[Form setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
        //NSDate *date =[Form dateFromString:[datearray objectAtIndex:i]];
        [Form setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
        NSDate *date =[Form dateFromString:[[datearray objectAtIndex:i] stringByReplacingOccurrencesOfString:@"/" withString:@"-"]];
        NSLog(@"%@",date);
        if(notification)
        {
            // un-comment this for current date time
            /*[notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:5*i]];
            NSLog(@" %@",[NSDate dateWithTimeIntervalSinceNow:5*i]);*/

            //comment this for current date time
            [notification setFireDate:date];
            NSLog(@" %@",date);

            //notification.fireDate = date;
            //if(notification.fireDate == date){
            //}

            notification.applicationIconBadgeNumber = i;
            notification.timeZone = [NSTimeZone defaultTimeZone];
            notification.alertBody = [NSString stringWithFormat:@"Today is %@\'s Birthday",[nameArray objectAtIndex:i]];
            notification.alertAction = @"View";
            notification.soundName = UILocalNotificationDefaultSoundName;
            [[UIApplication sharedApplication] scheduleLocalNotification:notification];
        }

        NSLog(@"fire date%@",  notification.fireDate);
    }
}

要使用5 sec间隔测试当前日期时间,您可以取消if(notification)之后的前两行注释,并在未注释的行后注释该行。