[英]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: 对于当您的应用程序在后台或在收到推送通知时未运行的情况,有两种情况:
remote-notification
is enabled in UIBackgroundModes. remote-notification
。 In this case, the above method may be called two times. UIApplicationStateBackground
. UIApplicationStateBackground
applicationState调用上述方法。 If user taps to presented push notification, the method will be called again, this time having applicationState value UIApplicationStateInactive
. UIApplicationStateInactive
。 remote-notification
is NOT enabled in 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.