简体   繁体   English

application:didReceiveRemoteNotification:fetchCompletionHandler Not Called

[英]application:didReceiveRemoteNotification:fetchCompletionHandler Not Called

It appears the function application:didReceiveRemoteNotification:fetchCompletionHandler is not called when the app has been forcefully quit. 看起来功能application:didReceiveRemoteNotification:fetchCompletionHandler当强制退出应用程序时,不会调用application:didReceiveRemoteNotification:fetchCompletionHandler 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? 有没有办法在后台唤醒应用程序,如果它尚未使用新的iOS 7远程通知后台模式运行?

application:didReceiveRemoteNotification:fetchCompletionHandler: gets called even if the app is suspended, not running at all, backgrounded, or active. application:didReceiveRemoteNotification:fetchCompletionHandler:即使应用程序被暂停,根本没有运行,后台运行或活动,也会被调用。 Also worth noting that the method is iOS 7 only. 另外值得注意的是,该方法仅适用于iOS 7。 Here is the apple documentation . 这是苹果文档

HOWEVER if the app was forcibly closed (ie by killing with the app switcher), the app will not be 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 (参见答案编辑:我在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 (ie you have to set the "content-available" flag within the push notification payload. See SO answer ). 当接收推送时,应用程序仅在“需要时”唤醒以调用application:didReceiveRemoteNotification:fetchCompletionHandler: method(即您必须在推送通知有效负载内设置“content-available”标志。参见SO回答 )。 The method will be called again if the user then opens the app by tapping the notification. 如果用户然后通过点击通知打开应用程序,则将再次调用该方法。

EDIT: haven't checked this on iOS 8. Has anyone else? 编辑:没有在iOS 8上检查过这个。还有其他人吗?

- (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;
}

The app should be launched even if it is not running. 应用程序即使没有运行也应该启动。 The Apple documentation says: Apple文档说:

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) (iOS App编程指南)

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) (UIApplicationDelegate协议参考)

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. 与应用程序:didReceiveRemoteNotification:方法不同,只有在应用程序运行时才会调用该方法,系统会调用此方法,而不管应用程序的状态如何。 If your app is suspended or not running , the system wakes up or launches your app and puts it into the background running state before calling the method. 如果您的应用暂停或未运行 ,系统会在调用该方法之前唤醒或启动您的应用并将其置于后台运行状态。 (UIApplicationDelegate Protocol Reference) (UIApplicationDelegate协议参考)

However when testing with "content-available":1 pushes the app is never launched when it is not running. 但是,当使用“content-available”进行测试时:1推送应用程序在未运行时永远不会启动。 When the app is suspended it works. 当应用程序暂停时,它可以工作。

Did you find yourself a solution Wes? 你发现自己是一个解决方案吗?

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将调用(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
}

As documented by Apple, the new multitasking API (fetch and remote-notification) will work only when the app in the suspended/background/foreground state. 正如Apple所记录的那样,新的多任务API(提取和远程通知)仅在应用程序处于挂起/后台/前台状态时才有效。

  • In the background/foreground state, then application:didReceiveRemoteNotification:fetchCompletionHandler will get triggered. 后台/前台状态,然后application:didReceiveRemoteNotification:fetchCompletionHandler将被触发。

  • In the Suspended state, then -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions will get triggered. Suspended状态下,然后-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions将触发-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

  • In the Not Running state (your case), application:didReceiveRemoteNotification:fetchCompletionHandler never gets triggered. Not Running状态(您的情况), application:didReceiveRemoteNotification:fetchCompletionHandler永远不会被触发。

Please refer to the Apple documentation for more about app states. 有关应用程序状态的更多信息,请参阅Apple文档

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 why it doesn't get launched. 事实上,你的武力使用退出将是为什么它不会推出的原因。

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])
}

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? 所以当应用程序被强制退出杀死时,我猜没有办法做任何事情?

暂无
暂无

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

相关问题 application:didReceiveRemoteNotification:fetchCompletionHandler:多次调用。如何避免? - application: didReceiveRemoteNotification: fetchCompletionHandler: called more than once.How to avoid? 检测是否通过点击Notification Center中的通知调用application:didReceiveRemoteNotification:fetchCompletionHandler: - Detect if application: didReceiveRemoteNotification: fetchCompletionHandler: was called by tapping on a notification in Notification Center Swift & Push Notification:application(_:didReceiveRemoteNotification:fetchCompletionHandler:) 没有被调用 - Swift & Push Notification : application(_:didReceiveRemoteNotification:fetchCompletionHandler:) does not get called applicationState在application:didReceiveRemoteNotification:fetchCompletionHandler中为UIApplicationStateInactive: - applicationState is UIApplicationStateInactive in application:didReceiveRemoteNotification:fetchCompletionHandler: 为什么didReceiveRemoteNotification没有被调用但是didReceiveRemoteNotification:当我的应用程序在前台时调用fetchCompletionHandler? - Why is didReceiveRemoteNotification not called but didReceiveRemoteNotification:fetchCompletionHandler called when my app is in the foreground? didReceiveRemoteNotification:fetchCompletionHandler:但是从未调用完成处理程序 - didReceiveRemoteNotification:fetchCompletionHandler: but the completion handler was never called didReceiveRemoteNotification fetchCompletionHandler没有在后台调用以进行静默推送 - didReceiveRemoteNotification fetchCompletionHandler is not getting called in background for silent push 应用程序委托收到了对 -application:didReceiveRemoteNotification:fetchCompletionHandler: 的调用,但从未调用过完成处理程序 - Application delegate received call to -application:didReceiveRemoteNotification:fetchCompletionHandler: but the completion handler was never called 为什么调用ReceiveRemoteNotification:fetchCompletionHandler被调用,但通常didReceiveRemoteNotification不是? - Why didReceiveRemoteNotification:fetchCompletionHandler is called but usual didReceiveRemoteNotification isn't? didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler未调用 - didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler not called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM