响应没有成功的回报。

发布于 2025-02-08 17:10:06 字数 5061 浏览 2 评论 0原文

收到响应时有一个问题,

response.body().success

它返回 0 ,但我希望 1 它显示了烤面包节目失败!如下面的代码,意味着响应不会成功

mService = Common.getFCMService();
private void sendNotificationOrder(String order_number) {
    DatabaseReference tokens = FirebaseDatabase.getInstance().getReference("Tokens");
    Query data = tokens.orderByChild("isServerToken").equalTo(true);
    data.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot postSnapShot:dataSnapshot.getChildren()) {
                Token serverToken = postSnapShot.getValue(Token.class);
                Notification notification = new Notification("Ziead's Company", "You have New Order" + order_number);
                Sender content = new Sender(serverToken.getToken(), notification);
                mService.sendNotification(content)
                        .enqueue(new Callback<MyResponse>() {
                            @Override
                            public void onResponse(Call<MyResponse> call, Response<MyResponse> response) {
                                try {
                                       //Here It is Returning "0" not 1                                        System.out.println(response.body().success);
                                        if (response.code() == 200) {
                                            if (response.body().success == 1) {
                                                System.out.println("Succeedd");
                                                Toast.makeText(Cart.this, "Thank you , Order Place", Toast.LENGTH_SHORT).show();
                                                finish();
                                            } else {
                                                Toast.makeText(Cart.this, "Failed !!!", Toast.LENGTH_SHORT).show();
                                            }
                                        }
                                    }
                                    catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                                @Override
                                public void onFailure(Call<MyResponse> call, Throwable t) {
                                Log.e("ERROR", t.getMessage());

                            }
                        });

            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });
}

界面

public class Token {
    private String token;
    private boolean isServerToken;

    public Token() {
    }

    public Token(String token, boolean isServerToken)
    {
        this.token = token;
        this.isServerToken = isServerToken;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public boolean isServerToken() {
        return isServerToken;
    }

    public void setServerToken(boolean serverToken) {
        isServerToken = serverToken;
    }
}

token.java:notification.java:sender.java:common.java:apiservice

public class Notification {
    public String body;
    public String title;

    public Notification(String body, String title) {
        this.body = body;
        this.title = title;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

这是与主代码相关的

public class Sender {
    public String to;
    public  Notification notification;

    public Sender(String to, Notification notification) {
        this.to = to;
        this.notification = notification;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public Notification getNotification() {
        return notification;
    }

    public void setNotification(Notification notification) {
        this.notification = notification;
    }
}

public class Common {
    public static APIService getFCMService(){
        return RetrofitClient.getClient(BASE_URL).create(APIService.class);
    }
}

reTRofitClient.java

public interface APIService {
    @Headers(
            {
                    "Content-Type:application/json",
                    "Authorization:key=AAAA0EXeo9E:APA91bGeAWN69zVVIKh9ib_XA6OkMnEA1S0_sZILrJ1civQVF-eelOAF4o3qCOTZpRgSwb9bySoU92ypchs5BqOE2P1Y_FPlvUanIrV2g7RPCY7IpGjYqSK1rlKVxODm6v8R"
            }
    )
    @POST("fcm/send")
    Call<MyResponse> sendNotification(@Body Sender body);
}

所有代码

There is a problem when receiving the response,

response.body().success

it returns 0 but I expect 1
it shows a toast shows Failed! As the code below which means the response doesn't succeed

mService = Common.getFCMService();
private void sendNotificationOrder(String order_number) {
    DatabaseReference tokens = FirebaseDatabase.getInstance().getReference("Tokens");
    Query data = tokens.orderByChild("isServerToken").equalTo(true);
    data.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot postSnapShot:dataSnapshot.getChildren()) {
                Token serverToken = postSnapShot.getValue(Token.class);
                Notification notification = new Notification("Ziead's Company", "You have New Order" + order_number);
                Sender content = new Sender(serverToken.getToken(), notification);
                mService.sendNotification(content)
                        .enqueue(new Callback<MyResponse>() {
                            @Override
                            public void onResponse(Call<MyResponse> call, Response<MyResponse> response) {
                                try {
                                       //Here It is Returning "0" not 1                                        System.out.println(response.body().success);
                                        if (response.code() == 200) {
                                            if (response.body().success == 1) {
                                                System.out.println("Succeedd");
                                                Toast.makeText(Cart.this, "Thank you , Order Place", Toast.LENGTH_SHORT).show();
                                                finish();
                                            } else {
                                                Toast.makeText(Cart.this, "Failed !!!", Toast.LENGTH_SHORT).show();
                                            }
                                        }
                                    }
                                    catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                                @Override
                                public void onFailure(Call<MyResponse> call, Throwable t) {
                                Log.e("ERROR", t.getMessage());

                            }
                        });

            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });
}

Token.java:

public class Token {
    private String token;
    private boolean isServerToken;

    public Token() {
    }

    public Token(String token, boolean isServerToken)
    {
        this.token = token;
        this.isServerToken = isServerToken;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public boolean isServerToken() {
        return isServerToken;
    }

    public void setServerToken(boolean serverToken) {
        isServerToken = serverToken;
    }
}

Notification.java:

public class Notification {
    public String body;
    public String title;

    public Notification(String body, String title) {
        this.body = body;
        this.title = title;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

Sender.java:

public class Sender {
    public String to;
    public  Notification notification;

    public Sender(String to, Notification notification) {
        this.to = to;
        this.notification = notification;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public Notification getNotification() {
        return notification;
    }

    public void setNotification(Notification notification) {
        this.notification = notification;
    }
}

Common.java:

public class Common {
    public static APIService getFCMService(){
        return RetrofitClient.getClient(BASE_URL).create(APIService.class);
    }
}

APIService Interface:

public interface APIService {
    @Headers(
            {
                    "Content-Type:application/json",
                    "Authorization:key=AAAA0EXeo9E:APA91bGeAWN69zVVIKh9ib_XA6OkMnEA1S0_sZILrJ1civQVF-eelOAF4o3qCOTZpRgSwb9bySoU92ypchs5BqOE2P1Y_FPlvUanIrV2g7RPCY7IpGjYqSK1rlKVxODm6v8R"
            }
    )
    @POST("fcm/send")
    Call<MyResponse> sendNotification(@Body Sender body);
}

RetrofitClient.java:

This is all the code related to the main code

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文