Giter Club home page Giter Club logo

Comments (2)

andrei-m-code avatar andrei-m-code commented on July 22, 2024

@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..

from net-core-push-notifications.

Thomr77 avatar Thomr77 commented on July 22, 2024

@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!

from net-core-push-notifications.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.