简体   繁体   English

本地通知在iOS中不起作用

[英]local notifications not working in iOS

I'm trying yo store my notifications in a database after scheduling them. 我正在尝试在安排通知后将通知存储在数据库中。 For the life of me, I can't get the notification to show up in the iOS simulator. 为了我的一生,我无法在iOS模拟器中显示通知。 Any idea what's wrong?Here's the app delegate code for setting notifications. 知道怎么了吗?这是用于设置通知的应用程序委托代码。 Basically, events are set to be weekly, and the descriptor string is of the form "Lecture on Monday from 5:04 AM to 6:09 AM". 基本上,事件设置为每周一次,并且描述符字符串的格式为“周一下午5:04 AM至6:09 AM演讲”。 I then add the current date(without time) to the time to set the notification before storing in the database. 然后,我将当前日期(无时间)添加到设置通知的时间,然后再存储到数据库中。 The notification doesn't fire up, but if I show the stuff in the notifications database in a table view it looks like (null)(null) 通知不会启动,但是如果我在表视图中显示通知数据库中的内容,它看起来像(null)(null)

    -(void)applicationDidBecomeActive:(UIApplication *)application
{
    NSArray *arrrayToSetNotifications= nil;
    arrrayToSetNotifications= self.getAlleventsToday;
    if (arrrayToSetNotifications!=nil)
    {


        for (Events *event in arrrayToSetNotifications)
        {

            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"dd.MM.yyyy HH:mm a"];

            [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT+5:30"]];

            NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
            [dateFormatter2 setDateFormat:@"dd.MM.yyyy"];
            [dateFormatter2 setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT+5:30"]];

            NSDate *now=[NSDate date];
            NSString *todaysdate=[dateFormatter2 stringFromDate:now];


            NSDate *datefromstring = [[NSDate alloc] init];
            // voila!
            NSArray *words=[event.descriptorString componentsSeparatedByString:@" "];
            NSString *dateString=[NSString stringWithFormat:@"%@ %@ %@",todaysdate,[words objectAtIndex:4],[words objectAtIndex:5]];
            datefromstring=[dateFormatter dateFromString:dateString];

            UILocalNotification *notification=[[UILocalNotification alloc]init];
            notification.fireDate=datefromstring;
            notification.repeatInterval=NSWeekCalendarUnit;
            [application scheduleLocalNotification:notification];


            AppDelegate *appDelegate =
            [[UIApplication sharedApplication] delegate];

            NSManagedObjectContext *context =
            [appDelegate managedObjectContext];
            NSManagedObject *notification2;
            [notification2 setValue:datefromstring forKey:@"date"];
            [notification2 setValue:0 forKey:@"attendedValue"];
            [notification2 setValue:event forKey:@"event"];
            notification2 = [NSEntityDescription
                            insertNewObjectForEntityForName:@"Notifications"
                            inManagedObjectContext:context];
            NSError *error;
            [context save:&error];
        }
    }

}

At the very least, you need a lower case hh for the time field, rather than an upper case because you're parsing with a 12-hour time and an AM / PM . 至少,对于时间字段,您需要使用小写hh ,而不是大写,因为您要解析12小时的时间和AM / PM you use HH for 24 hour time parsing and display. 您使用HH进行24小时的时间解析和显示。

When you try to parse using both, it seems to get epically confused for the hour; 当您尝试同时使用两者进行解析时,似乎在一个小时内会感到困惑; eg 例如

NSString *toParse = @"2014-01-20 08:00 AM";
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
[fmt setDateFormat:@"yyyy-MM-dd HH:mm a"];
NSDate *dateFromString = [fmt dateFromString:toParse];
NSLog(@"%@", dateFromString);
[fmt setDateFormat:@"yyyy-MM-dd hh:mm a"];
dateFromString = [fmt dateFromString:toParse];
NSLog(@"%@", dateFromString);

Your second problem is that you're not actually storing any useful information in the core data store. 第二个问题是您实际上没有在核心数据存储区中存储任何有用的信息。 Your code starts trying to setValue on an uninitialized NSManagedObject *notification2 . 您的代码开始尝试在未初始化的NSManagedObject *notification2上设置setValue This should be swapped around to: 应将其交换为:

NSManagedObject *notification2 = [NSEntityDescription
         insertNewObjectForEntityForName:@"Notifications"
         inManagedObjectContext:context];
[notification2 setValue:datefromstring forKey:@"date"];
[notification2 setValue:0 forKey:@"attendedValue"];
[notification2 setValue:event forKey:@"event"];

That way the data from parsing is stored in the created insertion object rather than being discarded. 这样,来自解析的数据将存储在创建的插入对象中,而不是被丢弃。

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

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