Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firebase Error Throws Exception #80

Closed
Thomr77 opened this issue Mar 11, 2023 · 10 comments
Closed

Firebase Error Throws Exception #80

Thomr77 opened this issue Mar 11, 2023 · 10 comments

Comments

@Thomr77
Copy link
Contributor

Thomr77 commented Mar 11, 2023

Hi,

When Firebase encounters an error then an exception is thrown - in previous versions it reported the error.

Call FirebaseSender.SendAsync(payload) with an invalid recipient token for example.

try
{
    FirebaseResponse firebase_response = await firebase_sender.SendAsync(payload));
}
catch (Exception ex)
{
    Debug.WriteLine(ex.ToString());
}

Thanks!

@andrei-m-code
Copy link
Owner

@Thomr77 it seems like we were always throwing errors when firebase returns non-successful response:

https://github.com/andrei-m-code/net-core-push-notifications/blob/v3.1.0/CorePush/Google/FcmSender.cs#L80

However as we're now using their new API some statuses/behaviours changed. I can't tell..

@Thomr77
Copy link
Contributor Author

Thomr77 commented Mar 15, 2023

@andrei-m-code cheers!

It would be so convenient if it came back as an object (error) so that there is less code on the caller's side to take it apart.

What do you think about replacing

public class FirebaseResponse
{
    public string Name { get; set; }
}

with

public class FirebaseResponse
{
    public class FirebaseError
    {
        public class Detail
        {
            public string Type { get; set; }
            public string ErrorCode { get; set; }
        }

        public int Code { get; set; }
        public string Message { get; set; }
        public string Status { get; set; }
        public Detail[] Details { get; set; }
    }

    public FirebaseError Error { get; set; }

    public bool Success => Error == null;
}

And then just deserialize the response every time

public async Task<FirebaseResponse> SendAsync(object payload, CancellationToken cancellationToken = default)
{
    var json = serializer.Serialize(payload);

    using var message = new HttpRequestMessage(
        HttpMethod.Post, 
        $"https://fcm.googleapis.com/v1/projects/{settings.ProjectId}/messages:send");

    var token = await GetJwtTokenAsync();
            
    message.Headers.Add("Authorization", $"Bearer {token}");
    message.Content = new StringContent(json, Encoding.UTF8, "application/json");

    using var response = await http.SendAsync(message, cancellationToken);
    var responseString = await response.Content.ReadAsStringAsync(cancellationToken);

    //if (!response.IsSuccessStatusCode)
    //{
    //    throw new HttpRequestException("Firebase notification error: " + responseString);
    //}

    return serializer.Deserialize<FirebaseResponse>(responseString);
}

Then it is ready to go for the caller.

Could that work?

I can add this code if this works for you - but I have not done that before so not sure - please advise.

Thanks!

@andrei-m-code
Copy link
Owner

That would be handy but I'd need to verify that the structure of the error is a constant to be sure we don't break anything. I'm fine if you open Merge Request with supporting links to the documentation.

@Thomr77
Copy link
Contributor Author

Thomr77 commented Sep 23, 2024

Hey @andrei-m-code!

I just started with integrating the above code but then I saw that you have unified the results from SendAsync() with ApnSender and FirebaseSender into PushResult record.

I think it is really great to unify these results, but I am not sure that I can add the above code now as it is only really working with Firebase.

Please let me know how to proceed - thanks!

@andrei-m-code
Copy link
Owner

@Thomr77 PushResult is a way to go. Let me know if you come across any issues. cc @StanDotNet

@Thomr77
Copy link
Contributor Author

Thomr77 commented Sep 23, 2024

@andrei-m-code - Okay sounds great.

With these updates, you have anyway done pretty much what the above code would do! 😀

@Thomr77 Thomr77 closed this as completed Sep 23, 2024
@Thomr77 Thomr77 reopened this Sep 23, 2024
@Thomr77
Copy link
Contributor Author

Thomr77 commented Sep 24, 2024

Hey @andrei-m-code!

I have a small suggestion so the Firebase SendAsync() PushResult is more quickly consumable by the caller.

Possibly, I could add more detail to the FirebaseError for the future.

The changes are in two files - can I commit these for you to review?

Please advise - thanks!

@andrei-m-code
Copy link
Owner

@Thomr77 go for it! Thank you!

@andrei-m-code
Copy link
Owner

Released. Thank you!

@Thomr77
Copy link
Contributor Author

Thomr77 commented Sep 27, 2024

@andrei-m-code - Awesome, thank you too! 😀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants