因此,我有一个在iOS上运行的帮助程序方法,用于在应用程序中执行后台任务。该调用将一条消息发送到我们的后端,该消息将通知推送到设备以打开一个单独的应用,用户点击“接受”即可进行两因素验证。
问题是,当SimpleCall运行时,用户点击通知(或主页),任务立即被取消。在以前的iOS / Xamarin版本中使用的逻辑已不再使用。我试图在较低级别的代码上运行后台帮助程序,但结果是相同的。
是否有更好的方法来执行此类任务,也许是因为它正在阻止网络通话?
呼叫看起来像这样:
public async Task<Response> SimpleCall()
{
return await GetNetworkResponseAsync(() => {
var response = _loginHelper.Tasks.User.WaitForAppPush(...);
return response;
});
}
public override async Task RunInBackground(string name, Func<Task> taskToKeepRunning)
{
nint taskId = 0;
taskId = UIApplication.SharedApplication.BeginBackgroundTask(name, new Action(() =>
{
Debug.WriteLine("Exhausted time");
UIApplication.SharedApplication.EndBackgroundTask(taskId);
}));
await taskToKeepRunning.Invoke();
Debug.WriteLine("Completed task");
UIApplication.SharedApplication.EndBackgroundTask(taskId);
}
public void Run(){
Response result = null;
var act = new Func<Task>(async () =>
{
try
{
var pushResult = await _authService.SimpleCall();
result = pushResult?.Data ?? null;
}
catch(Exception ex)
{
Debug.WriteLine("Act Call:" + ex.Message);
}
});
if (Device.RuntimePlatform == Device.iOS)
{
await _backgroundService.RunInBackground("Login", act);
}
}