ios - 应用程序在后台时来自 iOS 推送通知操作的 Http 请求
问题描述
我的 iOS 应用程序可以接收带有操作按钮的推送通知:
按钮点击通过 UrlSession 和数据任务发送 post http 请求。当应用程序暂停或未运行时,它运行良好。但是当应用程序在后台时它不起作用,例如当用户刚刚从底部向上滑动(或按下主页按钮)时。推送操作句柄,但未发送 http 请求。
从我的 iPhone 的控制台 os_logs 中,我看到 UrlSession 无法从后台建立连接并抛出错误代码 = -1005“网络连接丢失。”。只要我杀死应用程序,它就可以。我尝试了不同的 url 会话配置、后台 url 会话、后台任务,但没有任何效果。我发现的唯一解决方法是使用exit(0)
fromapplicationDidEnterBackground
方法,但强烈建议不要以编程方式杀死应用程序。
问题:当应用程序在后台时,如何通过推送通知操作建立 http 连接?
我的代码:
class NotificationService: NSObject, UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// notification processing if app is in foreground
...
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let requestId = String(describing: response.notification.request.content.userInfo["requestId"] ?? "")
switch response.actionIdentifier {
case "CONFIRM_ACTION":
confirmAuthenticationRequest(requestId: requestId, isConfirmed: true)
case "REJECT_ACTION":
confirmAuthenticationRequest(requestId: requestId, isConfirmed: false)
default:
// on notification tap
...
}
completionHandler()
}
private func confirmAuthenticationRequest(requestId: String, isConfirmed: Bool) {
var request = URLRequest(url: url)
request.httpMethod = "POST"
let json: [String: Any] = [
"requestId": requestId,
"deny": !isConfirmed
]
request.httpBody = try? JSONSerialization.data(withJSONObject: json)
request.addValue("application/json", forHTTPHeaderField: "content-type")
let dataTask = urlSession.dataTask(with: request)
dataTask.resume()
}
}
解决方案
最后我找到了答案。推送通知操作不稳定的原因是completionHandler
从userNotificationCenterDidReceiveResponse
方法调用的错误方式。当执行像 http 请求这样的异步操作时,您必须completionHandler
在操作完成后(而不是在操作调用之后)并从主线程调用。这是工作示例:
class NotificationService: NSObject, UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let requestId = String(describing: response.notification.request.content.userInfo["requestId"] ?? "")
switch response.actionIdentifier {
case "CONFIRM_ACTION":
confirmAuthenticationRequest(requestId: requestId, isConfirmed: true, completion: completionHandler)
case "REJECT_ACTION":
confirmAuthenticationRequest(requestId: requestId, isConfirmed: false, completion: completionHandler)
default:
// on notification tap
...
completionHandler()
}
}
private func confirmAuthenticationRequest(requestId: String, isConfirmed: Bool, completion: @escaping () -> Void) {
var request = URLRequest(url: url)
request.httpMethod = "POST"
let json: [String: Any] = [
"requestId": requestId,
"deny": !isConfirmed
]
request.httpBody = try? JSONSerialization.data(withJSONObject: json)
request.addValue("application/json", forHTTPHeaderField: "content-type")
let dataTask = urlSession.dataTask(with: request) { _, _, _ in
DispatchQueue.main.async {
completion()
}
}
dataTask.resume()
}
}
推荐阅读
- python - 如何转换表单中的字典项,以便在 python 3 中可以将其作为类属性接受?
- python-2.7 - 如何在最短路径中找到特定点/坐标?
- javascript - 如何仅使用 ember js 将 type="number" 设为正数
- ios - 我想创建一个通用类数组,但它强制我指定它的类型
- java - 如何标记括号?
- java - Java 8 Stream 从对象映射创建一个对象
- android - ANDROID_ID 调用空对象错误 Firebase 数据库
- php - 我的 php 代码说我在数据库中没有任何内容,尽管我有。我的代码有什么问题?
- windows - 如何防止 Windows 10 在我的设备上安装游戏?
- google-app-maker - AppMaker 表中的条件格式