繁体   English   中英

本地通知在iOS中不起作用

[英]local notifications not working in iOS

我正在尝试在安排通知后将通知存储在数据库中。 为了我的一生,我无法在iOS模拟器中显示通知。 知道怎么了吗?这是用于设置通知的应用程序委托代码。 基本上,事件设置为每周一次,并且描述符字符串的格式为“周一下午5:04 AM至6:09 AM演讲”。 然后,我将当前日期(无时间)添加到设置通知的时间,然后再存储到数据库中。 通知不会启动,但是如果我在表视图中显示通知数据库中的内容,它看起来像(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];
        }
    }

}

至少,对于时间字段,您需要使用小写hh ,而不是大写,因为您要解析12小时的时间和AM / PM 您使用HH进行24小时的时间解析和显示。

当您尝试同时使用两者进行解析时,似乎在一个小时内会感到困惑; 例如

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);

第二个问题是您实际上没有在核心数据存储区中存储任何有用的信息。 您的代码开始尝试在未初始化的NSManagedObject *notification2上设置setValue 应将其交换为:

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

这样,来自解析的数据将存储在创建的插入对象中,而不是被丢弃。

暂无
暂无

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

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