【问题标题】:Push Notification -didFinishLaunchingWithOptions推送通知 -didFinishLaunchingWithOptions
【发布时间】:2014-01-13 07:28:19
【问题描述】:

当我发送推送通知并且我的应用程序处于打开状态或在后台并单击推送通知时,我的应用程序将重定向到 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 是您故事板中最初的viewController?实际上...截取情节提要的屏幕截图并将其包含在答案中。
  • 没有错误,如果应用程序没有打开,当推送消息来时我点击消息应用程序停留在主视图控制器中,因为 if(apsInfo) 块在这种情况下不起作用。跨度>
  • hm... 而不是[self.window.rootViewController presentViewController:pvc animated:YES completion:NULL]; 试试这个[self.window setRootViewController:pvc]。 (重新编译,在设备上运行...但不要误会,这不是您问题的确切答案,只是检查if(apsInfo) 块是否执行)跨度>
  • 非常感谢,正确答案就是你的答案。它现在正在工作。再次感谢您。

标签: ios cocoa-touch storyboard push-notification


【解决方案1】:

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

第 1 部分:设置一个类(在您的情况下为 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:我还没有为我的项目做过这个(),但它很有效,是我能想到的最好的方法(目前 >)

【讨论】:

  • 感谢您的回答,我在代码中应用了您的回答,如果打开应用程序,推送消息到达时 PushMessagesVc 会显示没有标题栏。另一方面,如果应用程序不工作,则消息到达应用程序会异常关闭。
  • @cdsoft : 标题栏?你想要navBar?嗯...我们正在使用您的-presentViewController:animated:completion: 方法,所以我们可以推送,而不是展示,所以...尝试[self.navigationController pushViewController:pvc animated:YES]; |||至于if the application is not working, whe message arrive application is closed outomatically...如果应用程序不工作如何自动关闭?
  • 如果我在下面添加,应用程序关闭 [self performSelector:@selector(onLoadPushNotification) withObject:nil afterDelay:1];
  • @cdsoft : 哦,崩溃了……方法名onLoadPushNotification不正确(我的错误),应该是postNotificationToPresentPushMessagesVC。我会更新答案
  • 真的很棒。经过长时间的尝试,它很快解决了我的问题。
【解决方案2】:

斯威夫特 5

didFinishLaunchingWithOptions中获取推送通知字典,当应用程序被杀死并接收推送通知并且用户点击它时

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

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

【讨论】:

    【解决方案3】:

    斯威夫特 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
    

    }

    【讨论】:

      【解决方案4】:

      斯威夫特 5

      我在 didFinishLaunchingWithOptions 中将通知写入 var "notification",如果 "notification" != nil 启动了我在 applicationDidBecomeActive 中的推送,然后将其删除 (= nil)

      class AppDelegate: UIResponder, UIApplicationDelegate {
      
      var notification: [AnyHashable : Any]? = nil
      
      func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
      
          registerForPushNotifications()
      
          //write notification to var
          if launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] != nil {
              self.notification = launchOptions![UIApplication.LaunchOptionsKey.remoteNotification] as? [AnyHashable : Any]
          }
      
          return true
      }
      
      func applicationDidBecomeActive(_ application: UIApplication) {
          if notification != nil {
          //for launch notification after terminate app
              NotificationCenter.default.post(name: Notification.Name(rawValue: "startPush"), object: nil)
              notification = nil
          }
      }
      

      【讨论】:

        【解决方案5】:

        Swift 版本:

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

        【讨论】:

          猜你喜欢
          • 2012-04-04
          • 1970-01-01
          • 1970-01-01
          • 2018-02-17
          • 2021-06-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多