菜鸟教程小白 发表于 2022-12-13 00:02:54

ios - 从静默远程通知加载数据


                                            <p><p>我有一个应用程序,当它通过 <code>-</code> 处于非事件状态时接收静默推送。推送负载包含一个我需要预取的 url,以便在下次应用启动时准备好数据。</p>

<p>下载完成后应用需要调用<code>completionHandler</code>:</p>

<blockquote>
<p>The block to execute when the download operation is complete. When calling this block, pass in the fetch result value that best describes the results of your download operation. You must call this handler and should do so as soon as possible. For a list of possible values, see the UIBackgroundFetchResult type.</p>
</blockquote>

<p>问题是我是否可以做一个简单的 <code>NSURLSession</code> 请求,或者我是否应该使用后台提取 <a href="https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html" rel="noreferrer noopener nofollow">as described here</a> 之一进行提取</p>

<p><strong>选项1:</strong>使用简单的<code>NSURLSession</code>并调用回调</p>

<pre><code>- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{
    NSURL *url = ];
    NSURLSession *session = ];
    NSURLSessionDataTask *task = completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

      // save the result &amp; call the
      completionHandler(data ? UIBackgroundFetchResultNewData : UIBackgroundFetchResultNoData);

    }];
    ;
}
</code></pre>

<p><strong>选项 2:</strong>使用额外的后台处理来下载内容</p>

<pre><code>- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{
    NSURLSessionDataTask *task;

    __block UIBackgroundTaskIdentifier backgroundId = [ beginBackgroundTaskWithExpirationHandler:^{

      // time&#39;s up, cancel the download

      ;
      backgroundId = UIBackgroundTaskInvalid;
      completionHandler(UIBackgroundFetchResultFailed);

      ;

    }];

    NSURL *url = ];
    NSURLSession *session = ];

    task = completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
      // check if time was up
      if(backgroundId == UIBackgroundTaskInvalid) {
            return;
      }

      ;
      backgroundId = UIBackgroundTaskInvalid;

      // save the result &amp; call the
      completionHandler(data ? UIBackgroundFetchResultNewData : UIBackgroundFetchResultNoData);

    }];
    ;
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>所以回答我自己的问题,经过一些测试,看来 <strong>选项 2</strong> 工作得很好。我可以使用 <code>UIBackgroundTaskIdentifier</code> 下载我需要的任何数据。如果我不使用它,下载失败</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 从静默远程通知加载数据,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42534582/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42534582/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 从静默远程通知加载数据