0

下面是我在互联网上检查过的代码,一切似乎都很好,但不知道通知没有触发我知道它很简单,但我仍然被困在这里,请帮忙

    -(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 总是会打印其他东西,那么它只会显示正确的值}

}

在我的应用程序中

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


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

1 回答 1

1

看起来这是您使用的日期格式的问题。我成功了,GMT现在setDateFormat:@"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间隔进行测试,您可以取消注释后的前 2 行,if(notification)并在未注释的行之后注释该行。

于 2013-07-23T06:07:54.153 回答