Claudio
Claudio

Reputation: 2037

UILocalNotification - Error on opening app from notification

I am using local notifications in my app. It's firing ok, but when I open the app from the notification, I can't access notification's userinfo. This is how I am scheduling the notification:

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];
}

This is how I am handling when I open the app from the 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];
    }
}

And this is what I get from LLDB when I am debugging didFinishLaunchingWithOptions:

(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;
}}

My question is: what am I doing wrong? Is there any way to convert this UIConcreteLocalNotification to NSDictionary? I have tried to convert it to UILocalNotification too, but I had no success.

Regards!

Upvotes: 1

Views: 923

Answers (2)

SS Akhtar
SS Akhtar

Reputation: 346

You have not implemented

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

Implement this method and try.

Upvotes: 0

Adeel Ur Rehman
Adeel Ur Rehman

Reputation: 566

Do like this and it will work fine:

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

Upvotes: 2

Related Questions