内容可用静默通知未调用正确的 AppDelegate 方法

Content-available silent notification not calling correct AppDelegate method

我添加了一些登录功能,用于使用内容可用通知下载相当大的内容包到我们的应用程序,我注意到一些行为有点奇怪,我不确定如何处理它正确:

  1. 如果在应用程序长时间处于后台时收到通知,据我所知,application:didReceiveRemoteNotification:fetchCompletionHandler 方法被正确调用。
  2. 偶尔(尚未解决,但似乎是在离开应用程序后的某个时间内)而不是调用上述方法,应用程序在后台启动并且 application:didFinishLaunchingWithOptions 是使用 remoteNotification 密钥调用,并且永远不会调用上述方法。

场景 2. 是预期的行为吗?我在网上看不到任何地方(Apple 论坛、文档等)说这可能发生,但它显然是!

如果 2. 发生,我的计划是检测通知是否是无声的内容可用通知和 运行 相同的后台下载逻辑(伪代码),如下所示:

var launchedByContentPush: Bool = false
if let remoteNotification = launchOptions?[.remoteNotification] as? [AnyHashable : Any], let aps = remoteNotification["aps"] as? [AnyHashable : Any] {
    launchedByContentPush = aps.keys.count == 1 && aps["content-available"] as? Int == 1
}

if launchedByContentPush {
 // 1. Don't do normal app-setup stuff which shouldn't be done
 // 2. Download content
}

这是一个明智的计划,还是我最终会破坏 1. 在某些用户实际启动应用程序的情况下?

只是为了 100% 确认:我正在收到内容可用的通知,这方面的一切都在进行中,所以据我所知,这不是配置问题!

显示了一些日志,其中一个是完美运行的会话,另一个是调用 applicationDidFinishLaunching 的会话

不正确

2020-04-02T19:01:03Z [INFO] | com.threesidedcube.ThunderCloud (ContentController) | Initialising Content Controller
2020-04-02T19:01:03Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Setting app build date to 2 April 2020 at 19:42:56
2020-04-02T19:01:03Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | <REDACTED>
2020-04-02T19:01:03Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | No files exist in streamed bundle directory subfolder: languages
Error: The folder “languages” doesn’t exist.
2020-04-02T19:01:03Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | No files exist in streamed bundle directory subfolder: languages
Error: The folder “languages” doesn’t exist.
2020-04-02T19:01:03Z [INFO] | com.threesidedcube.ThunderCloud (AppState) | application:DidFinishLaunchingWithOptions with keys: [__C.UIApplicationLaunchOptionsKey(_rawValue: UIApplicationLaunchOptionsRemoteNotificationKey)]
2020-04-02T19:01:03Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | No files exist in streamed bundle directory subfolder: languages
Error: The folder “languages” doesn’t exist.
2020-04-02T19:01:03Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | No files exist in streamed bundle directory subfolder: languages
Error: The folder “languages” doesn’t exist.
2020-04-02T19:01:03Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | No files exist in streamed bundle directory subfolder: languages
Error: The folder “languages” doesn’t exist.
2020-04-02T19:01:03Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | `appLaunched` called
2020-04-02T19:01:03Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Checking for app upgrade
2020-04-02T19:01:03Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Optionally checking for updated content
2020-04-02T19:01:03Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Checking for updates with timestamp: 1585848393.0
2020-04-02T19:05:48Z [INFO] | com.threesidedcube.ThunderCloud (AppState) | applicationWillEnterForeground
2020-04-02T19:05:48Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Checking for updates failed: The request timed out.
2020-04-02T19:05:49Z [INFO] | com.threesidedcube.ThunderCloud (AppState) | applicationDidBecomeActive
2020-04-02T19:08:33Z [INFO] | com.threesidedcube.ThunderCloud (AppState) | applicationWillResignActive
2020-04-02T19:08:34Z [INFO] | com.threesidedcube.ThunderCloud (AppState) | applicationDidEnterBackground

正确

2020-04-02T23:19:12Z [INFO] | com.threesidedcube.ThunderCloud (ContentController) | Initialising Content Controller
2020-04-02T23:19:12Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Setting app build date to 2 April 2020 at 19:42:56
2020-04-02T23:19:12Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Base URL configured as: <REDACTED>
2020-04-02T23:19:12Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | No files exist in streamed bundle directory subfolder: languages
Error: The folder “languages” doesn’t exist.
2020-04-02T23:19:12Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | No files exist in streamed bundle directory subfolder: languages
Error: The folder “languages” doesn’t exist.
2020-04-02T23:19:12Z [INFO] | com.threesidedcube.ThunderCloud (AppState) | application:DidFinishLaunchingWithOptions with keys: []
2020-04-02T23:19:12Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | No files exist in streamed bundle directory subfolder: languages
Error: The folder “languages” doesn’t exist.
2020-04-02T23:19:12Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | No files exist in streamed bundle directory subfolder: languages
Error: The folder “languages” doesn’t exist.
2020-04-02T23:19:12Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | No files exist in streamed bundle directory subfolder: languages
Error: The folder “languages” doesn’t exist.
2020-04-02T23:19:12Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | `appLaunched` called
2020-04-02T23:19:12Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Checking for app upgrade
2020-04-02T23:19:12Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Optionally checking for updated content
2020-04-02T23:19:12Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Checking for updates with timestamp: 1585848393.0
2020-04-02T23:19:12Z [INFO] | com.threesidedcube.ThunderCloud (AppState) | applicationDidBecomeActive
2020-04-02T23:19:13Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | No update found
2020-04-02T23:20:36Z [INFO] | com.threesidedcube.ThunderCloud (AppState) | applicationWillResignActive
2020-04-02T23:20:37Z [INFO] | com.threesidedcube.ThunderCloud (AppState) | applicationDidEnterBackground
2020-04-02T23:35:57Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Push notification received, checking if it's a `content-available` push
2020-04-02T23:35:57Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | content-available == 1 sending notification off to `ContentController`
2020-04-02T23:35:57Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | `appLaunched` called
2020-04-02T23:35:57Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Checking for app upgrade
2020-04-02T23:35:57Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Handling content-available notification
2020-04-02T23:35:57Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Making sure notification bundle isn't after a landmark publish this app shouldn't receive
2020-04-02T23:35:57Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | No landmark timestamp provided in notification
2020-04-02T23:35:57Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Checking notification timestamp against latest on-disk bundle version
2020-04-02T23:35:57Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Downloading content-available bundle with timestamp: 1685827838.0
2020-04-02T23:35:57Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Downloading bundle: <REDACTED>
Destination: file:///var/mobile/Containers/Data/Application/CBCB1B36-D853-4E36-B766-9F3F31158DF7/Library/Application%20Support/StormDeltaBundle/
2020-04-02T23:36:04Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Handling events for background url session: 784B47D4-C2B6-4310-ADD5-8539BE40A447-60638-000035ACB72C6167
2020-04-02T23:36:04Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Starting background request controller
2020-04-02T23:36:06Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Unpacking bundle...
2020-04-02T23:36:06Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | data.tar.gz read to data object
2020-04-02T23:36:06Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Attempting to gunzip data from data.tar.gz
2020-04-02T23:36:07Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Gunzip successful
2020-04-02T23:36:09Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Bundle tar saved to disk
2020-04-02T23:36:09Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Attempting to untar the bundle
2020-04-02T23:36:12Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Untar successful
2020-04-02T23:36:12Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Verifying bundle...
2020-04-02T23:36:12Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Loading manifest.json into JSON object
2020-04-02T23:36:12Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Loading manifest.json as dictionary
2020-04-02T23:36:12Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | app.json exists
2020-04-02T23:36:12Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | manifest.json exists
2020-04-02T23:36:12Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Verifying pages
2020-04-02T23:36:12Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Verifying languages
2020-04-02T23:36:12Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Verifying content
2020-04-02T23:36:13Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Bundle is valid
2020-04-02T23:36:13Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Cleaning up `data.tar.gz` and `data.tar` files
2020-04-02T23:36:13Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Copying bundle
From: file:///private/var/mobile/Containers/Data/Application/CBCB1B36-D853-4E36-B766-9F3F31158DF7/tmp/StormDeltaBundle/
To: file:///var/mobile/Containers/Data/Application/CBCB1B36-D853-4E36-B766-9F3F31158DF7/Library/Application%20Support/StormDeltaBundle/
2020-04-02T23:36:26Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Beginning excluding from backup files in directory: /var/mobile/Containers/Data/Application/CBCB1B36-D853-4E36-B766-9F3F31158DF7/Library/Application Support/StormDeltaBundle/content
2020-04-02T23:36:29Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Beginning excluding from backup files in directory: /var/mobile/Containers/Data/Application/CBCB1B36-D853-4E36-B766-9F3F31158DF7/Library/Application Support/StormDeltaBundle/languages
2020-04-02T23:36:29Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Beginning excluding from backup files in directory: /var/mobile/Containers/Data/Application/CBCB1B36-D853-4E36-B766-9F3F31158DF7/Library/Application Support/StormDeltaBundle/data
2020-04-02T23:36:31Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Beginning excluding from backup files in directory: /var/mobile/Containers/Data/Application/CBCB1B36-D853-4E36-B766-9F3F31158DF7/Library/Application Support/StormDeltaBundle/pages
2020-04-02T23:36:31Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Beginning excluding from backup files in directory: /var/mobile/Containers/Data/Application/CBCB1B36-D853-4E36-B766-9F3F31158DF7/Library/Application Support/StormDeltaBundle
2020-04-02T23:36:31Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Removing bundle in directory: file:///private/var/mobile/Containers/Data/Application/CBCB1B36-D853-4E36-B766-9F3F31158DF7/tmp/StormDeltaBundle/
2020-04-02T23:36:31Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Update complete, Refreshing language
2020-04-02T23:36:31Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | No files exist in streamed bundle directory subfolder: languages
Error: The folder “languages” doesn’t exist.
2020-04-02T23:36:31Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | No files exist in streamed bundle directory subfolder: languages
Error: The folder “languages” doesn’t exist.
2020-04-02T23:36:31Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | Removing bundle in directory: file:///private/var/mobile/Containers/Data/Application/CBCB1B36-D853-4E36-B766-9F3F31158DF7/tmp/StormDeltaBundle/
2020-04-02T23:36:33Z [DEBUG] | com.threesidedcube.ThunderCloud (ContentController) | No files exist in streamed bundle directory subfolder: pages
Error: The folder “pages” doesn’t exist.

Paul 上面的评论是正确的,如果在收到 content-available 通知之前应用程序因任何原因终止,将调用 didFinishLaunching 方法是预期的行为。