Open a view controller when a iOS push notification is received

前端 未结 4 1892
挽巷
挽巷 2020-11-29 01:09

I want to open a specific view controller when a user clicks on the received push notification message, but when I receive a push notification message and click the message,

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 01:34

    I was having same problem that when app is suspended/terminated and push notification arrives my app was only opening and not redirecting to specific screen corresponding to that notification the solution is,

    in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions this method the parameter launchOptions tells us if it has the notification by checking that we need to call the method to redirect to specific screen

    the code is as below...

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        //your common or any code will be here at last add the below code..
    
        NSMutableDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    
        if (notification)
        {
    //this notification dictionary is same as your JSON payload whatever you gets from Push notification you can consider it as a userInfo dic in last parameter of method -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
            NSLog(@"%@",notification);
            [self showOfferNotification:notification];
        }
    
         return YES;
    }
    

    then in the method showOfferNotification:notification you can redirect user to corresponding screen like...

    //** added code for notification
    -(void)showOfferNotification:(NSMutableDictionary *)offerNotificationDic{
    //This whole is my coding stuff.. your code will come here..
        NSDictionary *segueDictionary = [offerNotificationDic valueForKey:@"aps"];
    
        NSString *segueMsg=[[NSString alloc]initWithFormat:@"%@",[segueDictionary valueForKey:@"alert"]];
    
        NSString *segueID=[[NSString alloc]initWithFormat:@"%@",[offerNotificationDic valueForKey:@"id"]];
    
        NSString *segueDate=[[NSString alloc]initWithFormat:@"%@",[offerNotificationDic valueForKey:@"date"]];
    
        NSString *segueTime=[[NSString alloc]initWithFormat:@"%@",[offerNotificationDic valueForKey:@"time"]];
    
        NSLog(@"Show Offer Notification method : segueMsg %@ segueDate %@ segueTime %@ segueID %@",segueMsg,segueDate,segueTime,segueID);
    
        if ([segueID isEqualToString:@"13"]){
    
            NSString *advertisingUrl=[[NSString alloc]initWithFormat:@"%@",[offerNotificationDic valueForKey:@"advertisingUrl"]];
    
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults setObject:segueMsg forKey:@"notificationMsg"];
            [defaults setObject:segueDate forKey:@"notifcationdate"];
            [defaults setObject:segueTime forKey:@"notifcationtime"];
            [defaults setObject:advertisingUrl forKey:@"advertisingUrl"];
            [defaults synchronize];
    
            navigationController = (UINavigationController *)self.window.rootViewController;
            UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle: nil];
            FLHGAddNotificationViewController *controller = (FLHGAddNotificationViewController*)[mainStoryboard instantiateViewControllerWithIdentifier: @"offerViewController"];
            [navigationController pushViewController:controller animated:YES];
        }
    }
    

提交回复
热议问题