Trj*_*Trj 1 geolocation push-notification apple-push-notifications ios
我正在尝试找出涉及后台任务的iOS上实际可能的功能。我有一个在后台运行并跟踪设备上位置更改的应用程序。
现在,是否可以将推送通知发送到设备并将设备将当前位置发送到服务器,而无需用户打开应用程序使其在前台运行?
我知道这个问题有点老,但是我要为那些像我一样在提出问题后很久才来到这里的人作答。
现在,是否可以将推送通知发送到设备并将设备将当前位置发送到服务器,而无需用户打开应用程序使其在前台运行?
是。您现在可以执行此操作。我目前正在我的应用中执行类似的操作。现在还有其他一些SO问题可以正确说明这一点。这是一
基本上,你必须启用Background Execution
了Remote Notifications
。您可以通过转到应用程序的目标在XCode中执行此操作。打开Capabilities
启用该Background Modes
功能的标签,然后检查Remote Notifications
清单中的项目。(我假设您已经启用了该Push Notifications
功能)。
使用此代码,您用于在前台处理通知的代码也将在后台为您服务,因为该application:didReceiveRemoteNotification:fetchCompletionHandler
方法将在您的应用程序在后台时被调用。这是Swift 3.0中的示例代码:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
// Print full message.
print(userInfo)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// Print full message.
print(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
Run Code Online (Sandbox Code Playgroud)
您可能想查看有关如何在后台处理网络请求的SO问题(和可接受的答案)。后台请求无法执行Alamofire Swift
查看有关背景执行的文档。特别是有关推送通知的部分
归档时间: |
|
查看次数: |
3552 次 |
最近记录: |