当应用程序未运行状态时点击通知时如何导航到特定的目标页面?

Posted

技术标签:

【中文标题】当应用程序未运行状态时点击通知时如何导航到特定的目标页面?【英文标题】:How to navigate to particular objectivec page when tap on notification that too when app is not running state? 【发布时间】:2017-08-04 11:04:03 【问题描述】:

我的应用程序处于未运行状态我点击通知其打开的主页但我想打开特定页面,但它适用于应用程序处于前台和后台时,但我在应用程序委托的一种方法中编写了此代码警报视图显示所有状态。

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void (^)())completionHandler
         

          NSDictionary *userInfo = response.notification.request.content.userInfo;
           NSDictionary *dict = userInfo[kAps];
           NSString *str = [NSString stringWithFormat:@"%@",[dict valueForKey:kAlert]];
           UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
           MyOrderDetailVC *view =  [[UIStoryboard storyboardWithName:kMainStoryboard bundle:nil] instantiateViewControllerWithIdentifier:@"MyOrderDetailVC"];

           NSString *orderstatus = userInfo[kgcmnotificationorderstatus];
           NSString *ordertype = userInfo[kgcmnotificationordertype];
           view.isFromSideMenu = YES;
           view.isfromViewOrders = YES;
           view.status =  orderstatus;  //[NSString stringWithFormat:@"%@",[dictOrderDetails valueForKey:korderstatus]];
           view.isProduct =ordertype;
           view.strOrderStatus = orderstatus;
           view.strProgramId = @"-1";
           if([ordertype isEqualToString: @"0"])
           
           view.isBookedOrders = NO;
           
           if([ordertype isEqualToString: @"1"])
           
             view.isBookedOrders =YES;
           
           if([orderstatus isEqualToString:@"0"])
           
             view.strOrderStatus = kPending;
           
           else if([orderstatus isEqualToString:@"1"])
           
             view.strOrderStatus = kConfirmed;
           
           else if ([orderstatus isEqualToString:@"2"])
           
             view.strOrderStatus = kCompleted;
           
           [navController.visibleViewController.navigationController pushViewController:view animated:YES];

     [[Shared sharedInstance] showAlertViewInViewController:kAppName message:str buttonTitles:@[OK] viewC:self.window.rootViewController handler:nil];


【问题讨论】:

【参考方案1】:

当您的应用未运行并且您想在didFinishLaunchingWithOptions 方法中检测您的Notification 时,如下所示

-(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
    
  UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];    
  if (localNotif) 
     // Notification detect here
     // Put your code here so it will be call when your App will not running state and tapped on Notification.
      

【讨论】:

但它会“- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler”这个方法,因为它显示警报视图在此方法中调用,但不导航到仅在此方法中写入的其他页面。 您可以通过 if ( application.applicationState == UIApplicationStateInactive) //当应用在后台时从推送通知中打开 获取应用状态【参考方案2】:

在 didFinishLaunchingWithOptions 检查通知是或注意

NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (notification)

    NSLog(@"app recieved notification %@",notification);
  // open your particular controller


else
    NSLog(@"app did not recieve  notification");

【讨论】:

我试过这个,但当我点击通知时,它会转到主页并显示警报视图,但不会导航到特定页面。 你可以打开 partilcaur 视图控制器作为这个 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil]; Yourcontroller *myVC = (Yourcontroller *)[storyboard instantiateViewControllerWithIdentifier:@"YourcontrollerId"]; [self.navigationController pushViewController: myVC animated:YES];

以上是关于当应用程序未运行状态时点击通知时如何导航到特定的目标页面?的主要内容,如果未能解决你的问题,请参考以下文章

Swiftui:当用户点击推送通知时,如何导航到特定的 NavigationLink?

当应用程序被终止状态时,点击推送通知后无法导航到 ViewController

当应用程序处于未运行或后台状态时,如何处理推送通知有效负载而不点击它

在 Android 中点击 One Signal 推送通知时如何导航到特定页面?

Swift导航到View from notification

当应用程序处于非运行状态时,推送通知委托触发的解决方案是啥?