推送通知-didFinishLaunchingWithOptions

当我发送一个推送通知,我的应用程序打开或在后台,我点击推送通知,我的应用程序redirect到PushMessagesVc viewController按照预期

我使用如下代码:

 -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; PushMessagesVc *pvc = [mainstoryboard instantiateViewControllerWithIdentifier:@"PushMessagesVc"]; [self.window.rootViewController presentViewController:pvc animated:YES completion:NULL]; } 

在上面的代码/场景中没有问题,但如果应用程序已closures,并且单击推送通知,则应用程序在此情况下不redirect我的PushMessagesVc viewControllerPushMessagesVc应用程序停留在主屏幕上。

对于第二种情况,我使用下面的代码:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { sleep(1); [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeNone)]; [UIApplication sharedApplication].applicationIconBadgeNumber = 1; NSDictionary *userInfo = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"]; NSDictionary *apsInfo = [userInfo objectForKey:@"aps"]; if(apsInfo) { UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; PushMessagesVc* pvc = [mainstoryboard instantiateViewControllerWithIdentifier:@"PushMessagesVc"]; [self.window.rootViewController presentViewController:pvc animated:YES completion:NULL]; return YES; } return YES; } 

但在这种情况下, PushMessagesVc不会出现。

由于您只想在获取推送通知时显示viewController ,因此您可以尝试将NSNotificationCenter用于您的目的:

第1部分:build立一个类( 在你的情况下, rootViewController )来监听/响应NSNotification

假设MainMenuViewController是你的navigationControllerrootViewController
设置这个类来收听NSNotification

 - (void)viewDidLoad { //... [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(presentMyViewOnPushNotification) name:@"HAS_PUSH_NOTIFICATION" object:nil]; } -(void)presentMyViewOnPushNotification { //The following code is no longer in AppDelegate //it should be in the rootViewController class (or wherever you want) UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; PushMessagesVc *pvc = [mainstoryboard instantiateViewControllerWithIdentifier:@"PushMessagesVc"]; [self presentViewController:pvc animated:YES completion:nil]; //either presentViewController (above) or pushViewController (below) //[self.navigationController pushViewController:pvc animated:YES]; } 

第2部分:发布通知( 可能来自代码中的任何地方

在你的情况下, AppDelegate.m方法应该如下所示

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //firstly, don't sleep the thread, it's pointless //sleep(1); //remove this line if (launchOptions) { //launchOptions is not nil NSDictionary *userInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; NSDictionary *apsInfo = [userInfo objectForKey:@"aps"]; if (apsInfo) { //apsInfo is not nil [self performSelector:@selector(postNotificationToPresentPushMessagesVC) withObject:nil afterDelay:1]; } } return YES; } -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { //this method can be done using the notification as well [self postNotificationToPresentPushMessagesVC]; } -(void)postNotificationToPresentPushMessagesVC { [[NSNotificationCenter defaultCenter] postNotificationName:@"HAS_PUSH_NOTIFICATION" object:nil]; } 

PS:我还没有为我的项目做过( 但是 ),但它的工作原理,是我能想到做这种事情的最好方式( 目前

Swift 2.0对于“未运行”状态(本地和远程通知)

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Handle notification if (launchOptions != nil) { // For local Notification if let localNotificationInfo = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification { if let something = localNotificationInfo.userInfo!["yourKey"] as? String { self.window!.rootViewController = UINavigationController(rootViewController: YourController(yourMember: something)) } } else // For remote Notification if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as! [NSObject : AnyObject]? { if let something = remoteNotification["yourKey"] as? String { self.window!.rootViewController = UINavigationController(rootViewController: YourController(yourMember: something)) } } } return true 

}

Swift 3要获取推送通知字典在didFinishLaunchingWithOptions当应用程序被杀死,并推送通知接收和用户点击

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { if let userInfo = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String: AnyObject] { if let aps1 = userInfo["aps"] as? NSDictionary { print(aps1) } } return true } 

推送通知字典将显示在警报中。

Swift版本:

  if let localNotification: UILocalNotification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification { //launchOptions is not nil self.application(application, didReceiveLocalNotification: localNotification) }