ios – 推送通知-didFinishLaunchingWithOptions

前端之家收集整理的这篇文章主要介绍了ios – 推送通知-didFinishLaunchingWithOptions前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我发送推送通知,我的应用程序是开放的或在后台,我点击推送通知,我的应用程序重定向到PushMessagesVc viewController(如预期)

我使用如下代码

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

在上面的代码/场景中没有问题,但如果应用程序关闭,并且我点击推送通知,应用程序不会重定向我的PushMessagesVc viewController在这种情况&应用程序停留在主屏幕上.

对于第二种情况,我使用以下代码

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  2. {
  3. sleep(1);
  4. [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeNone)];
  5. [UIApplication sharedApplication].applicationIconBadgeNumber = 1;
  6.  
  7. NSDictionary *userInfo = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
  8. NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
  9.  
  10. if(apsInfo) {
  11. UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
  12. PushMessagesVc* pvc = [mainstoryboard instantiateViewControllerWithIdentifier:@"PushMessagesVc"];
  13. [self.window.rootViewController presentViewController:pvc animated:YES completion:NULL];
  14. return YES;
  15. }
  16. return YES;
  17. }

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

解决方法

由于您只希望在收到推送通知时提供viewController,您可以尝试使用NSNotificationCenter来实现:

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

假设MainMenuViewController是您的navigationController的rootViewController.
设置这个类来听NSNotification:

  1. - (void)viewDidLoad {
  2. //...
  3. [[NSNotificationCenter defaultCenter] addObserver:self
  4. selector:@selector(presentMyViewOnPushNotification)
  5. name:@"HAS_PUSH_NOTIFICATION"
  6. object:nil];
  7. }
  8.  
  9. -(void)presentMyViewOnPushNotification {
  10. //The following code is no longer in AppDelegate
  11. //it should be in the rootViewController class (or wherever you want)
  12.  
  13. UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
  14. PushMessagesVc *pvc = [mainstoryboard instantiateViewControllerWithIdentifier:@"PushMessagesVc"];
  15.  
  16. [self presentViewController:pvc animated:YES completion:nil];
  17. //either presentViewController (above) or pushViewController (below)
  18. //[self.navigationController pushViewController:pvc animated:YES];
  19. }

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

在您的情况下,AppDelegate.m方法应如下所示:

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  2. //firstly,don't sleep the thread,it's pointless
  3. //sleep(1); //remove this line
  4.  
  5. if (launchOptions) { //launchOptions is not nil
  6. NSDictionary *userInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
  7. NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
  8.  
  9. if (apsInfo) { //apsInfo is not nil
  10. [self performSelector:@selector(postNotificationToPresentPushMessagesVC)
  11. withObject:nil
  12. afterDelay:1];
  13. }
  14. }
  15. return YES;
  16. }
  17.  
  18. -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
  19. //this method can be done using the notification as well
  20. [self postNotificationToPresentPushMessagesVC];
  21. }
  22.  
  23. -(void)postNotificationToPresentPushMessagesVC {
  24. [[NSNotificationCenter defaultCenter] postNotificationName:@"HAS_PUSH_NOTIFICATION" object:nil];
  25. }

PS:我还没有为我的项目做这件事,但它的工作,是我可以想到这样做的最好的方式(暂时)

猜你在找的iOS相关文章