当我发送推送通知,我的应用程序是开放的或在后台,我点击推送通知,我的应用程序重定向到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];
- }
在上面的代码/场景中没有问题,但如果应用程序关闭,并且我点击推送通知,应用程序不会重定向我的PushMessagesVc viewController在这种情况&应用程序停留在主屏幕上.
对于第二种情况,我使用以下代码:
- - (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部分:设置一个类(在你的情况下,rootViewController)来监听/响应NSNotification
假设MainMenuViewController是您的navigationController的rootViewController.
设置这个类来听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];
- }
在您的情况下,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:我还没有为我的项目做这件事,但它的工作,是我可以想到这样做的最好的方式(暂时)