ios - 当应用程序未运行时,通过静默推送通知在后台更新本地数据库
<p><p>是否可以在用户无需点击通知(通知中心没有通知,因为它是静默推送通知)。
该应用程序根本没有运行(例如,用户强制退出)。我已经尝试了下面的代码,如果应用程序正在运行或在后台运行,它可以工作,但如果应用程序被强制退出(根本不运行),我也希望它工作。</p>
<p>我已启用后台获取和远程通知。</p>
<pre><code>func application(application: UIApplication, didReceiveRemoteNotification userInfo: , fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
handleNotification()
}
//this works, the someData is not a problem
func handleNotification() -> Void{
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setValue(someData, forKey: "key")
defaults.synchronize()
}
</code></pre>
<p><strong>当我发送推送通知时,如果应用程序处于事件状态或在后台,数据会更新,但如果应用程序根本没有运行,则数据不会更新。</strong></p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>简答:<strong>当应用强制退出(未运行)时,如果没有用户交互,(几乎)无法做任何事情。</strong></p>
<p>解释见文档:</p>
<ul>
<li><p> <a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html?hl=ro#//apple_ref/occ/intfm/UIApplicationDelegate/application:didReceiveRemoteNotification:fetchCompletionHandler:" rel="noreferrer noopener nofollow">Handling Remote Notifications - application:didReceiveRemoteNotification:fetchCompletionHandler</a> :</p>
<blockquote>
<p>Use this method to process incoming remote notifications for your app.
Unlike the application:didReceiveRemoteNotification: method, which is
called only when your app is running in the foreground, the system
calls this method when your app is running in the foreground or
background. In addition, if you enabled the remote notifications
background mode, the system launches your app (or wakes it from the
suspended state) and puts it in the background state when a remote
notification arrives. <strong>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</strong>.</p>
</blockquote> </li>
<li><p> <a href="https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/doc/uid/TP40007072-CH4-SW7" rel="noreferrer noopener nofollow">Understanding When Your App Gets Launched into the Background</a> :</p>
<blockquote>
<p>Apps that support background execution may be relaunched by the system
to handle incoming events. <strong>If an app is terminated for any reason
<em>other than the user force quitting it</em>, the system launches the app
when one of the following events happens</strong>:</p>
<ul>
<li>For location apps: The system receives a location update that meets
the app’s configured criteria for delivery. The device entered or
exited a registered region. (Regions can be geographic regions or
iBeacon regions.) </li>
<li>For audio apps, the audio framework needs the app to
process some data. (Audio apps include those that play audio or use
the microphone.) </li>
<li>For Bluetooth apps: An app acting in the central role
receives data from a connected peripheral. An app acting in the
peripheral role receives commands from a connected central. </li>
<li>For background download apps: A push notification arrives for an app and
the payload of the notification contains the content-available key
with a value of 1. The system wakes the app at opportunistic moments
to begin downloading new content. </li>
<li>For apps downloading content in the
background using the NSURLSession class, all tasks associated with
that session object either completed successfully or received an
error. A download initiated by a Newsstand app finishes. In most
cases, the system does not relaunch apps after they are force quit by
the user. </li>
</ul>
<p><strong>One exception is location apps, which in iOS 8 and later are
relaunched after being force quit by the user.</strong> In other cases, though,
the user must launch the app explicitly or reboot the device before
the app can be launched automatically into the background by the
system.</p>
</blockquote> </li>
</ul>
<p>如您所见,唯一的异常(exception)是位置应用程序,但这对您没有帮助。
请处理以下事实,<strong>这是一个操作系统限制,您无能为力。</strong></p></p>
<p style="font-size: 20px;">关于ios - 当应用程序未运行时,通过静默推送通知在后台更新本地数据库,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/31501279/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/31501279/
</a>
</p>
页:
[1]