我正在尝试通过调用lambda函数来获取数据,以响应远程推送通知,而我的应用程序在后台。我的通知配置正确,并且当应用程序在后台时调用didReceiveRemoteNotification。
我在该方法中有以下代码:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let lambdaInvoker = AWSLambdaInvoker.default()
lambdaInvoker.invokeFunction("lambdaFunctionName", jsonObject: jsonObject).continueWith(block: {(task:AWSTask<AnyObject>) -> Any? in
if let error = task.error as NSError? {
print(task.error!.localizedDescription)
print(task.error!)
DispatchQueue.main.async(execute: {
if (error.domain == AWSLambdaInvokerErrorDomain) && (AWSLambdaInvokerErrorType.functionError == AWSLambdaInvokerErrorType(rawValue: error.code)) {
print("Function error: \(String(describing: error.userInfo[AWSLambdaInvokerFunctionErrorKey]))")
} else {
print("Error: \(error)")
}
})
return nil
}
// Handle response in task.result
DispatchQueue.main.async(execute: {
if let jsonArray = task.result as? NSArray {
// do stuff
}
})
return nil
})
}
然而,该块不在lambda函数中执行。我之前没有使用过后台提取,也不确定如何使用lambda函数进行此操作。
答案 0 :(得分:1)
我缺少的是完成处理程序。添加之后,代码和块按预期执行:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let lambdaInvoker = AWSLambdaInvoker.default()
lambdaInvoker.invokeFunction("lambdaFunctionName", jsonObject: jsonObject).continueWith(block: {(task:AWSTask<AnyObject>) -> Any? in
if let error = task.error as NSError? {
print(task.error!.localizedDescription)
print(task.error!)
DispatchQueue.main.async(execute: {
if (error.domain == AWSLambdaInvokerErrorDomain) && (AWSLambdaInvokerErrorType.functionError == AWSLambdaInvokerErrorType(rawValue: error.code)) {
print("Function error: \(String(describing: error.userInfo[AWSLambdaInvokerFunctionErrorKey]))")
completionHandler(UIBackgroundFetchResult.newData)
} else {
print("Error: \(error)")
completionHandler(UIBackgroundFetchResult.newData)
}
})
return nil
}
// Handle response in task.result
DispatchQueue.main.async(execute: {
if let jsonArray = task.result as? NSArray {
// do stuff
completionHandler(UIBackgroundFetchResult.newData)
}
})
return nil
})
}