Search code examples
iosuilocalnotificationdata-objects

iOS >> Local Notifications >> How to Connect a Local Notification a Specific Object?


In my app I keep an array of data objects; upon certain actions, a Data Object is creating a Local Notification. I wish to have the Local Notification be able to recognise the Object that created it, so when the user opens the notification - whether it's from a UIAlertView that pops if the Notification is fired when the app is active, or whether it's triggered by the Notification view that pops when the app is in the background - I can open a screen with the specific Object Data presented.

How do I define for a Local Notification instance a relevant Object of mine?


Solution

  • Try this way...

    NSDictionary *dict=[NSDictionary dictionaryWithObject:@"YOUR OBJECT" forKey:@"YOUR KEY"];

       UILocalNotification *localNotif = [[UILocalNotification alloc] init];
        localNotif.fireDate = Pre_date;
        localNotif.timeZone = [NSTimeZone defaultTimeZone];
    
        // Notification details
        localNotif.alertBody = [txtRemindetText text];
        // Set the action button
        localNotif.alertAction = @"View";
        localNotif.userInfo=dict;
        localNotif.soundName = UILocalNotificationDefaultSoundName;
    
        // Schedule the notification
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    

    AppDelegate.m

    - (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
        // Handle the notificaton when the app is running
        NSLog(@"Recieved Notification %@",notif);
        NSLog(@"%@",notif.userInfo);
        NSLog(@"%@",[notif.userInfo objectForKey:@"YOUR KEY"];
    }
    

    Let me know if you have any problem.