firebase / firebase-admin-dotnet

Firebase Admin .NET SDK
https://firebase.google.com/docs/admin/setup
Apache License 2.0
356 stars 128 forks source link

Attempt to send a message via FirebaseMessaging.SendAsync ends up with Timeout #396

Closed wtlgo closed 2 months ago

wtlgo commented 2 months ago

[READ] Step 1: Are you in the right place?

[REQUIRED] Step 2: Describe your environment

[REQUIRED] Step 3: Describe the problem

Steps to reproduce:

Whenever I try to send a notification via FirebaseMessaging.SendAsync I get a timeout Exception.

Here's how I get my messaging instance:

        var credential = ...; // Getting the private key from the database. It is the contents of the file that I get by clicking "Generate new private key" at https://console.firebase.google.com/project/[redacted for privacy]/settings/serviceaccounts/adminsdk

        var instanceName = GetInstanceName(credential);
        var app = FirebaseApp.GetInstance(instanceName) ?? FirebaseApp.Create(new AppOptions
        {
            Credential = GoogleCredential.FromJson(credential)
        }, instanceName);

        var messaging = FirebaseMessaging.GetMessaging(app);

Then I send the notification:

                await messaging.SendAsync(new Message
                {
                    Token = notification.DeviceToken,
                    Notification = new Notification
                    {
                        Title = "New Message!",
                        Body = notification.Text
                    }
                });

At this point, the thread hangs for a while and it throws an exception:

      System.Threading.Tasks.TaskCanceledException: The request was canceled due to the configured HttpClient.Timeout of 100 seconds elapsing.
       ---> System.TimeoutException: A task was canceled.
       ---> System.Threading.Tasks.TaskCanceledException: A task was canceled.
         at Google.Apis.Auth.OAuth2.TokenRefreshManager.GetAccessTokenForRequestAsync(CancellationToken cancellationToken)
         at Google.Apis.Auth.OAuth2.ServiceAccountCredential.GetAccessTokenForRequestAsync(String authUri, CancellationToken cancellationToken)
         at Google.Apis.Auth.OAuth2.ServiceCredential.GetAccessTokenWithHeadersForRequestAsync(String authUri, CancellationToken cancellationToken)
         at Google.Apis.Auth.OAuth2.ServiceCredential.InterceptAsync(HttpRequestMessage request, CancellationToken cancellationToken)
         at Google.Apis.Http.ConfigurableMessageHandler.CredentialInterceptAsync(HttpRequestMessage request, CancellationToken cancellationToken)
         at Google.Apis.Http.ConfigurableMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
         at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts,
 Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
         --- End of inner exception stack trace ---
         --- End of inner exception stack trace ---
         at System.Net.Http.HttpClient.HandleFailure(Exception e, Boolean telemetryStarted, HttpResponseMessage response, CancellationTokenSource cts, Cancell
ationToken cancellationToken, CancellationTokenSource pendingRequestsCts)
         at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts,
 Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
         at FirebaseAdmin.Util.ErrorHandlingHttpClient`1.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
         at FirebaseAdmin.Util.ErrorHandlingHttpClient`1.SendAndReadAsync(HttpRequestMessage request, CancellationToken cancellationToken)
         at FirebaseAdmin.Util.ErrorHandlingHttpClient`1.SendAndDeserializeAsync[TResult](HttpRequestMessage request, CancellationToken cancellationToken)    
         at FirebaseAdmin.Messaging.FirebaseMessagingClient.SendAsync(Message message, Boolean dryRun, CancellationToken cancellationToken)
         at FirebaseAdmin.Messaging.FirebaseMessaging.SendAsync(Message message, Boolean dryRun, CancellationToken cancellationToken)
         at FirebaseAdmin.Messaging.FirebaseMessaging.SendAsync(Message message, Boolean dryRun)
         at FirebaseAdmin.Messaging.FirebaseMessaging.SendAsync(Message message)
         at [Redacted for privacy].Service.NotificationService.HandleAndroid(DbContext db, Hotel hotel) in [Redacted for privacy]\Service\No
tificationService.cs:line 59
wtlgo commented 2 months ago

Not a framework issue. Changing my DNS from CloudFlare DNS to Google Public DNS made it work.