0

I'm trying to request some data from an API using HttpClient in DidReceiveNotificationRequest method (Xamarin iOS Notification Service Extensions project).

When the GetAsync (HttpClient) the extension just crashes.

Am I doing something wrong?

public override async void DidReceiveNotificationRequest(UNNotificationRequest request, Action<UNNotificationContent> contentHandler)
{
    ...

    using (HttpClient client = new HttpClient())
    {
        await client.GetAsync("https://google.com").ConfigureAwait(false); // crashes here
    }

    ...
}

I also tried to use a NSUrlSessionHandler as the HttpClient handler, but no success.

idenardi
  • 600
  • 1
  • 6
  • 20

1 Answers1

0

try use with Uri:

Uri uri = new Uri(string.Format(client.BaseAddress + resource, string.Empty));
HttpResponseMessage responseMessage = await client.GetAsync(uri);

and for iOS need add to .plist - NSLocalNetworkUsageDescription

maybe need add HttpMessageHandler:

 client = new HttpClient(CreateClientHandler()) { BaseAddress = new Uri($"https://{Address}:5001") };
....

private HttpClientHandler CreateClientHandler()
    {
       return new HttpClientHandler
       {
           ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; }
       };
    }
  • Thanks Denys, I tried your code but no success. I've noticed that if I avoid the async/await and use .Result the code worked. It seems something related to async code. – idenardi Feb 09 '22 at 16:23