objective-capple-push-notificationsappdelegatepayloadios8.3

How to read the payload in applaunchwithoption in iOS?


I have implemented remote push notification for my iOS app. and it has a custom payload. Like this one

 "aps": {
    "alert": "joetheman",
    "sound": "default"
},
"message": "Some custom message for your app",
"id": 1234

When click on the notification on the lock screen, according to the id I wanna load different screen of the app (when app is not running, If I got a notification and the id is 2, app should open with the BookingViewController opened). So how can I read the custom payload in my applaunchwithOption in AppDelegate and also in the notificationdidreceived delegate too.

Please help me. Thanks


Solution

  • You Can use Following Method to handel PUSH notification:---

    -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
    
    {
        NSLog(@"%@",userInfo);
        NSString *msg=[userInfo objectForKey:@"message"];
        //Application is Running
        if ( application.applicationState == UIApplicationStateActive ){
    
            if ([[userInfo objectForKey:@"id"]isEqualToString:@"1"]) {
    
            self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
            YourViewController *obj = [[YourViewController alloc]initWithNibName:@"YourViewController" bundle:nil];
    
            nav = [[UINavigationController alloc]initWithRootViewController:obj];
            [self.nav setNavigationBarHidden:YES];
            self.window.rootViewController = nav;
            [self.window makeKeyAndVisible];
    
            }
            else if ([[userInfo objectForKey:@"id"]isEqualToString:@"2"]) {                 
            self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
            YourViewController *obj = [[YourViewController alloc]initWithNibName:@"YourViewController" bundle:nil];
    
            nav = [[UINavigationController alloc]initWithRootViewController:obj];
            [self.nav setNavigationBarHidden:YES];
            self.window.rootViewController = nav;
            [self.window makeKeyAndVisible];
    
           }
       }
    //when Application in Background
       else{
           if ([[userInfo objectForKey:@"id"]isEqualToString:@"1"]) {
    
            self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
            YourViewController *obj = [[YourViewController alloc]initWithNibName:@"YourViewController" bundle:nil];
    
            nav = [[UINavigationController alloc]initWithRootViewController:obj];
            [self.nav setNavigationBarHidden:YES];
            self.window.rootViewController = nav;
            [self.window makeKeyAndVisible];
    
           }
    
           else if ([[userInfo objectForKey:@"id"]isEqualToString:@"2"]) {
            self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
            YourViewController *obj = [[YourViewController alloc]initWithNibName:@"YourViewController" bundle:nil];
    
            nav = [[UINavigationController alloc]initWithRootViewController:obj];
            [self.nav setNavigationBarHidden:YES];
            self.window.rootViewController = nav;
            [self.window makeKeyAndVisible];
    
           }               
        }
    }