【问题标题】:local notifications not working in iOS本地通知在 iOS 中不起作用
【发布时间】:2014-01-20 19:25:05
【问题描述】:

我正在尝试在安排通知后将通知存储在数据库中。对于我的生活,我无法让通知显示在 iOS 模拟器中。知道出了什么问题吗?这是用于设置通知的应用程序委托代码。基本上,事件设置为每周一次,描述符字符串的格式为“Lecture on Monday from 5:04 AM to 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];
        }
    }

}

【问题讨论】:

    标签: ios objective-c uilocalnotification


    【解决方案1】:

    至少,时间字段需要小写 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"];
    

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

    【讨论】:

    • 好的,修好了。没有骰子。我该怎么办?
    • 我已经更新了答案。您正在将数据存储到未分配的对象 (notification2),您需要将 insertNewObjectForEntityForName 移动到 setValue 调用之前,否则它们只会丢失在æther 中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2020-06-30
    • 1970-01-01
    • 2020-09-22
    相关资源
    最近更新 更多