我正在基于教程的项目上工作。
讲师使用Firebase向服务器应用程序[Android]发送通知。
在插入订单中,当我发送订单时,它什么也没做,但无论如何都会发送订单。

在客户端应用程序中,该客户端发送订单后,基于代码,当通知成功发送时,我应该收到一条敬酒消息。这是所有相关的代码。

IFCM接口:

public interface IFCMService {
@Headers(
        {

                "Content-Type:application/json",
                "Authorization:key=****"
        }
)
@POST("fom/send")
Call<MyResponse> sendNotification(@Body DataMessage body);
}


MyFirebaseMessaging.java

public class MyFirebaseMessaging extends FirebaseMessagingService {

@Override
public void onNewToken(String s) {
    super.onNewToken(s);
    if (Common.currentUser != null)
        updateTokenToFirebas(s);
}

private void updateTokenToFirebas(String token) {
    IDrinkShopAPI mService = Common.getAPI();
    mService.updateToken(Common.currentUser.getPhone(),
            token,
            "0")
            .enqueue(new Callback<String>() {
                @Override
                public void onResponse(Call<String> call, Response<String> response) {
                    Log.d("DEBUG",response.toString());
                }

                @Override
                public void onFailure(Call<String> call, Throwable t) {
                    Log.d("DEBUG",t.getMessage());
                }
            });

}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    if (remoteMessage.getData() !=null)
    {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
            sendNotificationAPI26(remoteMessage);
        else
            sendNotification(remoteMessage);
    }
}

private void sendNotificationAPI26(RemoteMessage remoteMessage) {
    //Get information from Message
    Map<String,String> data = remoteMessage.getData();
    String title = data.get("title");
    String message = data.get("message");

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri);

    NotificationManager noti = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    noti.notify(new Random().nextInt(),builder.build());
}

private void sendNotification(RemoteMessage remoteMessage) {

    Map<String,String> data = remoteMessage.getData();
    String title = data.get("title");
    String message = data.get("message");

    //From API Level 26 , we need implement Notification Channel
    NotificationHelper helper;
    Notification.Builder builder;

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    helper = new NotificationHelper(this);
    builder = helper.getDrinkShopNotification(title,message,defaultSoundUri);

    helper.getManager().notify(new Random().nextInt(),builder.build());
}
}


FCMClient.java

public class FCMClient {
private static Retrofit retrofit = null;

public static Retrofit getClient(String baseUrl)
{
    if (retrofit == null)
        retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    return retrofit;
}
}


通用.java

private static final String FCM_API = "https://fcm.googleapis.com/";

public static IFCMService getGetFCMService()
{
    return FCMClient.getClient(FCM_API).create(IFCMService.class);
}


CartActivity.java:

     private void sendOrderToServer(float sumPrice, List<Cart> carts, String orderComment, String orderAddress) {
    if (carts.size() > 0)
    {
        String orderDetail = new Gson().toJson(carts);

        mService.submitOrder(sumPrice,orderDetail,orderComment,orderAddress,Common.currentUser.getPhone())
                .enqueue(new Callback<OrderResult>() {
                    @Override
                    public void onResponse(Call<OrderResult> call, Response<OrderResult> response) {
                        sendNotificationToServer(response.body());
                    }

                    @Override
                    public void onFailure(Call<OrderResult> call, Throwable t) {
                        Toast.makeText(CartActivity.this, ""+t.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
    }
}
    private void sendNotificationToServer(final OrderResult orderResult) {

    mService.getToken("server_app_01","1")
            .enqueue(new Callback<Token>() {
                @Override
                public void onResponse(Call<Token> call, Response<Token> response) {
                    Map<String,String> contentSend = new HashMap<>();
                    contentSend.put("title","ATLASco");
                    contentSend.put("message","سفارش جدید دریافت شد"+orderResult.getOrderId());
                    DataMessage dataMessage = new DataMessage();
                    if (response.body().getToken() != null)
                        dataMessage.setTo(response.body().getToken());
                    dataMessage.setData(contentSend);

                    IFCMService ifcmService = Common.getGetFCMService();
                    ifcmService.sendNotification(dataMessage)
                            .enqueue(new Callback<MyResponse>() {
                                @Override
                                public void onResponse(Call<MyResponse> call, Response<MyResponse> response) {

                                    Toast.makeText(CartActivity.this, "Response" + response.body(), Toast.LENGTH_SHORT).show();

                                    if (response.code() == 200)
                                    {
                                        if (response.body().success == 1)
                                        {
                                            Toast.makeText(CartActivity.this, "submitted", Toast.LENGTH_SHORT).show();

                                            Common.cartRepository.emptyCart();
                                            finish();
                                        }
                                        else
                                        {
                                            Toast.makeText(CartActivity.this, "Error!", Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                }

                                @Override
                                public void onFailure(Call<MyResponse> call, Throwable t) {
                                    Toast.makeText(CartActivity.this, ""+t.getMessage(), Toast.LENGTH_SHORT).show();
                                }
                            });
                }

                @Override
                public void onFailure(Call<Token> call, Throwable t) {
                    Toast.makeText(CartActivity.this, ""+t.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });

}


在购物车中的上述代码中,我在if (response.code() == 200)之前添加了Toast消息,并且响应为Null。

有什么想法吗?我也有一个PHP API,我可以添加到此。

调试器:

java - 如何修复响应为null的集成Firebase应用程序(云消息)?-LMLPHP

最佳答案

您的IFCMService代码中有错字

使用以下代码更正的IFCMService代码

   public interface IFCMService {
@Headers(
        {

                "Content-Type:application/json",
                "Authorization:key=****"
        }
)
@POST("fcm/send")
Call<MyResponse> sendNotification(@Body DataMessage body);
}


在帖子中,您写了fom / send,这是错误的,这就是为什么您在调试中遇到404错误的原因。复制以上代码并粘贴到IFCMService接口中

07-24 14:51