ios 应用程序:didReceiveRemoteNotification:fetchCompletionHandler 未调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18856204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
application:didReceiveRemoteNotification:fetchCompletionHandler Not Called
提问by Wes Cossick
It appears the function application:didReceiveRemoteNotification:fetchCompletionHandler
is not called when the app has been forcefully quit. It was my impression that the function would be invoked no matter what state the app was in, but it appears that it is only called if the app is already running in the background. Is there a way to wake up an app in the background if it is not already running using the new iOS 7 remote notification background mode?
application:didReceiveRemoteNotification:fetchCompletionHandler
当应用程序被强制退出时,该函数似乎没有被调用。我的印象是无论应用程序处于什么状态都会调用该函数,但似乎只有在应用程序已经在后台运行时才会调用它。如果应用程序尚未使用新的 iOS 7 远程通知后台模式运行,有没有办法在后台唤醒应用程序?
回答by nvrtd frst
application:didReceiveRemoteNotification:fetchCompletionHandler:
gets called even if the app is suspended, not running at all, backgrounded, or active. Also worth noting that the method is iOS 7 only. Here is the apple documentation.
application:didReceiveRemoteNotification:fetchCompletionHandler:
即使应用程序挂起、根本不运行、后台运行或处于活动状态,也会被调用。另外值得注意的是,该方法仅适用于 iOS 7。这是苹果文档。
HOWEVER if the app was forcibly closed (i.e. by killing with the app switcher), the app will notbe launched.(see SO answer)
EDIT:I checked this again on iOS 7.1 to see if they fixed this, but it still remains the case that if the app is killed manually, app will NOT be woken up and application:didReceiveRemoteNotification:fetchCompletionHandler:
will not be called
但是,如果应用程序被强行关闭(即通过应用程序切换器杀死),则该应用程序将不会启动。(见SO 答案)
编辑:我在 iOS 7.1 上再次检查了这个,看看他们是否解决了这个问题,但它仍然是如果应用程序被手动杀死,应用程序将不会被唤醒并且application:didReceiveRemoteNotification:fetchCompletionHandler:
不会被调用
When receiving the push, the app is only woken up only "if needed" to call the application:didReceiveRemoteNotification:fetchCompletionHandler:
method (i.e. you have to set the "content-available" flag within the push notification payload. See SO answer). The method will be called againif the user then opens the app by tapping the notification.
接收推送时,应用程序仅在“如果需要”时才被唤醒以调用该application:didReceiveRemoteNotification:fetchCompletionHandler:
方法(即您必须在推送通知有效负载中设置“内容可用”标志。请参阅SO answer)。如果用户然后通过点击通知打开应用程序,将再次调用该方法。
EDIT: haven't checked this on iOS 8. Has anyone else?
编辑:没有在 iOS 8 上检查过这个。还有其他人吗?
回答by kid
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
//Remote Notification Info
NSDictionary * remoteNotifiInfo = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
//Accept push notification when app is not open
if (remoteNotifiInfo) {
[self application:application didReceiveRemoteNotification: remoteNotifiInfo];
}
return YES;
}
回答by Emiel
The app should be launched even if it is not running. The Apple documentation says:
即使应用程序没有运行,它也应该被启动。苹果文档说:
When this value is present and a push notification arrives on a device, the system sends the notification to your app (launching it if needed) and gives it a few moments to process > the notification before displaying anything to the user. (iOS App Programming Guide)
When a push notification arrives, the system displays the notification to the user and launches the app in the background (if needed)so that it can call this method. (UIApplicationDelegate Protocol Reference)
Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running, the system calls this method regardless of the state of your app. If your app is suspended or not running, the system wakes up or launches your appand puts it into the background running state before calling the method. (UIApplicationDelegate Protocol Reference)
当此值存在并且推送通知到达设备时,系统会将通知发送到您的应用程序(如果需要启动它),并在向用户显示任何内容之前给它一些时间来处理 > 通知。(iOS App 编程指南)
当推送通知到达时,系统向用户显示通知并 在后台启动应用程序(如果需要),以便它可以调用此方法。(UIApplicationDelegate 协议参考)
与 application:didReceiveRemoteNotification: 方法不同,该方法仅在您的应用程序运行时调用,无论您的应用程序处于何种状态,系统都会调用此方法。如果您的应用程序暂停或未运行,系统会在调用该方法之前唤醒或启动您的应用程序并将其置于后台运行状态。(UIApplicationDelegate 协议参考)
However when testing with "content-available":1 pushes the app is never launched when it is not running. When the app is suspended it works.
但是,当使用 "content-available":1 pushes 进行测试时,应用程序在未运行时永远不会启动。当应用程序暂停时,它可以工作。
Did you find yourself a solution Wes?
你找到自己的解决方案了吗?
回答by scrrr
When your app has been force-quitted that method is not called. Instead, as usual, (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
is called.
当您的应用程序被强制退出时,不会调用该方法。相反,像往常一样,(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
被调用。
If the app was opened by tapping "Open" in a notification-popup, the notification is inside launchOptions
.
如果应用程序是通过在通知弹出窗口中点击“打开”打开的,则通知位于 内launchOptions
。
Get the push-notification dictionary like this:
像这样获取推送通知字典:
NSDictionary * pushNotificationUserInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (pushNotificationUserInfo)
{
// call your handler here
}
回答by Dave Addey
If you force quit an app from the application switcher, it will not be woken in the background (via any means) until after the next time it is next launched. When you force quit an app, you are effectively saying to the OS that you do not want this app to run at all, even if a background event would normally have woken it.
如果您从应用程序切换器强制退出应用程序,它不会在后台(通过任何方式)被唤醒,直到下一次启动之后。当您强制退出应用程序时,您实际上是在告诉操作系统您根本不希望该应用程序运行,即使后台事件通常会唤醒它。
This is worth watching out for during testing, as you may have force quit the app in order to check that it gets launched via the push notification when the app is not running. In fact, your use of force quit will be the reason whyit doesn't get launched.
这在测试期间值得注意,因为您可能已经强制退出应用程序,以便在应用程序未运行时检查它是否通过推送通知启动。事实上,你的武力使用退出将是真正的原因为什么它不会启动。
回答by Nandha
As documented by Apple, the new multitasking API (fetch and remote-notification) will work only when the app in the suspended/background/foregroundstate.
正如 Apple 所记录的那样,新的多任务 API(获取和远程通知)仅在应用程序处于挂起/后台/前台状态时才起作用。
In the background/foregroundstate, then
application:didReceiveRemoteNotification:fetchCompletionHandler
will get triggered.In the Suspendedstate, then
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
will get triggered.In the Not Runningstate (your case),
application:didReceiveRemoteNotification:fetchCompletionHandler
never gets triggered.
在后台/前台状态,
application:didReceiveRemoteNotification:fetchCompletionHandler
则将被触发。在Suspended状态下, then
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
将被触发。在Not Running状态(你的情况)下,
application:didReceiveRemoteNotification:fetchCompletionHandler
永远不会被触发。
Please refer to the Apple documentationfor more about app states.
有关应用程序状态的更多信息,请参阅Apple 文档。
回答by Luai Kalkatawi
To whom concern in Swift 2.0 I've solved my problem like this is for the background
我在 Swift 2.0 中关心谁我已经解决了我的问题,这是为背景
if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {
pushNotificationAction(remoteNotification as [NSObject : AnyObject])
}
回答by HunTer DRF
I recently met the same problem, and I found Apple updated their documentation and says:
我最近遇到了同样的问题,我发现 Apple 更新了他们的文档并说:
However, the system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again.
但是,如果用户强制退出,系统不会自动启动您的应用程序。在这种情况下,用户必须重新启动您的应用程序或重新启动设备,然后系统才会尝试再次自动启动您的应用程序。
application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
应用程序(_:didReceiveRemoteNotification:fetchCompletionHandler:)
So I guess there's no way to do anything when the app is killed by force quit?
所以我想当应用程序被强制退出杀死时,没有办法做任何事情吗?