UILocalNotification - 从通知打开应用程序时出错

时间:2014-12-18 11:14:19

标签: ios notifications

我在我的应用中使用本地通知。这是好的,但是当我从通知中打开应用程序时,我无法访问通知的userinfo。这就是我安排通知的方式:

UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.userInfo = @{@"eventId" : eventID};

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale preferredLanguages] objectAtIndex:0]];

[dateFormatter setLocale:enUSPOSIXLocale];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.S"];

NSDate *dateStart = [dateFormatter dateFromString: [eventDictionary valueForKey:@"time_start"]];


NSDate *newDate = [[NSDate date] dateByAddingTimeInterval:15];

notification.fireDate = newDate;
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.alertAction = @"Open";
notification.alertBody = @"Some title";
notification.soundName = UILocalNotificationDefaultSoundName;


//cfilipi: todo: verify if event date is bigger than today
if ([newDate timeIntervalSinceNow] > 0) {
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

这是我从通知(AppDelegate.m)打开应用程序时的处理方式:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey] != nil){
        //even when I cast to NSDictionary, it gets other class type (UIConcreteLocalNotification, I can't find any information about this class)
        NSDictionary * aPush = (NSDictionary *)[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
        [self handleLocalNotification:aPush];
    }
}

-(void)handleLocalNotification:(NSDictionary *)localNotification{
    NSDictionary *userInfo = [localNotification objectForKey:@"userinfo"];
    NSString *eventId = [userInfo objectForKey:@"eventId"];
    if ([eventId length] >= 1) {
        //I use this key in some parts of my code
        [[NSUserDefaults standardUserDefaults] setObject:eventId forKey:@"eventId"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

这是我在调试didFinishLaunchingWithOptions时从LLDB获得的内容:

(lldb) po aPush
<UIConcreteLocalNotification: 0x145b2ba0>{fire date = quinta-feira, 18 de dezembro de 2014 08:47:13 Horário de Verão de Brasília, time zone = America/Sao_Paulo (BRST) offset -7200 (Daylight), repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = (null), user info = {
eventId = 21;
}}

我的问题是:我做错了什么?有没有办法将此UIConcreteLocalNotification转换为NSDictionary?我也尝试将它转换为UILocalNotification,但我没有成功。

问候!

2 个答案:

答案 0 :(得分:2)

这样做,它会正常工作:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
    UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
   if ( notification != nil ) 
    {
        [self handleLocalNotification:[notification userInfo]];
    }
}

答案 1 :(得分:0)

您尚未实施

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

实施此方法并尝试。