简体   繁体   English

调用此方法时应用程序的状态是什么?

[英]What are the app state/s when this method gets called?

I am implementing push notification in my iOS app. 我正在我的iOS应用程序中实现推送通知。

I am supposed to display some custom View Controller in case the user opens (supposing the app is already running in background) the app from a push notification. 我应该显示一些自定义的View Controller,以防用户从推送通知中打开该应用程序(假设该应用程序已在后台运行)。

I am implementing this method: 我正在实现此方法:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    UIApplicationState state = [[UIApplication sharedApplication] applicationState];

    if (state == UIApplicationStateActive)
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"PushNotificationReceived" object:self userInfo:userInfo];
    }
}

I checked the documentation, it says this method gets called whether app is in background or foreground. 我检查了文档,说应用程序是在后台还是在前台都将调用此方法。 The system may call it second time when the app is about to enter the foreground, so I am wondering, is the code inside if (state == UIApplicationStateActive) { } going to execute. 当应用程序即将进入前台时,系统可能会第二次调用它,所以我想知道,如果(state == UIApplicationStateActive){}要执行,其中的代码是否在执行。

Also, there is already some handling of the push notification inside application:didFinishLaunchingWithOptions:, which only gets called at app launch. 另外,在application:didFinishLaunchingWithOptions:内部已经有一些推送通知的处理,仅在应用启动时才被调用。 I guess I should remove it from there as, it may display that View Controller twice. 我想我应该从那里删除它,因为它可能会两次显示该View Controller。

Please let me know you opinion if you have experience with this issues. 如果您有此问题的经验,请告诉我您的意见。

actually I'm not getting your point. 其实我不明白你的意思。

didReceiveRemoteNotification is called ONLY if the app is active and NOT in background . 当应用程序处于活动状态且不后台 didReceiveRemoteNotification调用didReceiveRemoteNotification So why do you need to check the state ? 那么,为什么需要检查state

Then, if the app is in background the device receive the push notification, than if the user tap on that, it is opened the app and that method is called . 然后,如果应用程序在后台,则设备会收到推送通知,而如果用户点击该通知, 则会打开应用程序并调用该方法

Otherwise if you open the app without tap on the notification , that method it is not called . 否则,如果您在不点击通知的情况下打开应用程序,则不会调用该方法。

From Apple documentation for the related method: 从Apple文档获取相关方法:

//This method will be invoked even if the application was launched or resumed because of the remote notification. 
//The respective delegate methods will be invoked first.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler NS_AVAILABLE_IOS(7_0);

Therefore, if you implement the above delegate method, you should NOT handle push notifications in application:didFinishLaunchingWithOptions: because the above method will be called even if application is just launching because of the push notification. 因此,如果实现上述委托方法,你应该处理推通知application:didFinishLaunchingWithOptions:因为上面的方法将被调用,即使应用才刚刚启动,因为推送通知。

[[UIApplication sharedApplication] applicationState] will have value UIApplicationStateActive only if you application was in foreground at the time push notification is received. 仅当您的应用程序在收到推送通知时处于前台时, [[UIApplication sharedApplication] applicationState]值才为UIApplicationStateActive

For the case when your application was in background or not running when a push notification is received, there are two scenarios: 对于当您的应用程序在后台或在收到推送通知时未运行的情况,有两种情况:

  1. remote-notification is enabled in UIBackgroundModes. 在UIBackgroundModes中启用了remote-notification In this case, the above method may be called two times. 在这种情况下,上述方法可能被调用两次 When the push notification is just received to device, the above method will be called with applicationState having value UIApplicationStateBackground . 当刚刚将推送通知接收到设备时,将使用具有值UIApplicationStateBackground applicationState调用上述方法。 If user taps to presented push notification, the method will be called again, this time having applicationState value UIApplicationStateInactive . 如果用户点按显示的推送通知,则该方法将再次被调用,这次具有applicationState值UIApplicationStateInactive
  2. remote-notification is NOT enabled in UIBackgroundModes. UIBackgroundModes中未启用remote-notification In this case, the method will be called only when user tapped to presented push notification. 在这种情况下, 当用户点击显示的推送通知时才调用该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否有一个UIViewController方法,当应用程序移动到后台时会自动调用? - Is there a UIViewController method that gets automatically called when the app moves to the background? 从主屏幕加载应用时会调用什么函数? - What function gets called when loading app from homescreen? 终止iPhone应用程序时调用什么方法? - What method is called when iPhone app is terminated? MapKit中调用的此灰色弹出窗口到底是什么?点击时会调用什么方法? - What exactly is this grey colored popup called in MapKit & what method gets called when tapped? keyboardWillShow被调用其他应用程序的键盘 - keyboardWillShow gets called for other app's keyboards 调用textFieldDidEndEditing时,应用程序冻结 - When textFieldDidEndEditing gets called, the app freezes 应用程序进入后台时调用方法 - Methods gets called when app goes to the background 当应用程序在后台被系统终止时调用什么生命周期方法? - What lifecycle method is called when app is terminated by system in the background? 当我们双击主页按钮并在后台发送应用程序时,将调用哪种方法? - Which method gets called when we double tap the home button and send the app in the background? 当应用程序在IOS关闭时会调用什么委托方法? - what delegate method that will called when an app close in IOS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM