Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect from which UILocalNotification an application was opened

In some cases my iOS application has to trigger multiple UILocalNotification at the same time. I want to decide which UILocalNotification the user clicked. When a user is clicking on a UILocalNotification the application was inactive or in the background. The problem is that the method

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {

is called for each triggered UILocalNotification. So when the app becomes active this method is called multiple times since I received multiple UILocalNotification's. Is there a way to determine which UILocalNotification was the cause for the app to be opened? A check of applicationState is not working since all UILocalNotification's have been received when the application was inactive or in the background.

Thanks a lot!

Edit: As an far example: When you receive a WhatsApp message from two different groups A and B and select push notification from group A this one will immediately displayed after the application opens itself. The difference between WhatsApp and my use case is that I have local notifications.

like image 215
Robert Weindl Avatar asked Dec 04 '15 23:12

Robert Weindl


2 Answers

While sheduling the notification you can set the some unique id for notification userinfo.

UILocalNotification *notif = [[UILocalNotification alloc] init];
    notif.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
    notif.timeZone = [NSTimeZone defaultTimeZone];

// set the your data with unique id
    NSMutableDictionary *dict=[NSMutableDictionary new];
    [dict setObject:Id forKey:@"id"];

// assignt the dictionary to user info
    notif.userInfo=dict;


    notif.alertBody = @"test Notification";
    notif.soundName = UILocalNotificationDefaultSoundName;


    [[UIApplication sharedApplication] scheduleLocalNotification:notif];

you can get the userinfo from didReceiveLocalNotification like that

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    if ([[notification.userInfo valueForKey:@"id"] isEqualToString:@"1"])
    {
        NSLog(@"notification id %@",[notification.userInfo valueForKey:@"id"]);
    }
    else if ([[notification.userInfo valueForKey:@"id"] isEqualToString:@"2"])
    {
        NSLog(@"notification id %@",[notification.userInfo valueForKey:@"id"]);
    }

    ////// or /////

    if ([notification.userInfo valueForKey:@"id"] )
    {
        NSLog(@"id of notification %@",[notification.userInfo valueForKey:@"id"]);
    }

}

from didFinishLaunchingWithOptions

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey])
    {
       UILocalNotification *notif=[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
        NSLog(@"notif.userInfo  %@",notif.userInfo);

//        notif.userInfo  {
//            id = 2;
//        }

    } 


        return YES;
}
like image 116
Nitin Singh Avatar answered Oct 21 '22 00:10

Nitin Singh


You can use the launch options to get access to the dictionary that was passed with the notification and from there, depending on what data you give the local notification when you're setting it up, you can check the dictionary and see which notification the method is responding to.

like image 36
pbush25 Avatar answered Oct 21 '22 01:10

pbush25