首页 > 解决方案 > 仅在收到第一条消息后才允许第二条消息

问题描述

我正在尝试结合 2 Firebase Cloud 消息传递。我合并了 2 个项目,每个项目都有自己的 firebase 消息传递。我没有作为模块导入,而是创建了一个新包。任何帮助,将不胜感激。提前致谢。我尝试了“if else”、else 和许多其他组合。请帮忙,已经使用了一周并搜索许多论坛以找到结合 2 个 firebase 消息的答案。

//来自 MyFirebaseMessaging 类

@Override
    public void onMessageReceived(final RemoteMessage remoteMessage) {
        
        //copied from second project

        if(remoteMessage.getData() != null) {
            //Log.d("EDMTDEV", remoteMessage.getNotification().getBody());
            Map<String,String> data =  remoteMessage.getData(); // Get data from notification :(
            String customer = data.get("customer");
            String lat = data.get("lat");
            String lng=data.get("lng");


            Intent intent = new Intent(getBaseContext(), CustomerCall.class);
            intent.putExtra("lat", lat);
            intent.putExtra("lng", lng);
            intent.putExtra("customer", customer);

            startActivity(intent);
        }
        
        // second message after the first one above  "CustomerCall.class is active. this is a reply when a button is pressed on the above activity above has started, I DO NOT WANT IT TO START, only if a button is pressed on the above activity "CustomerCall.class"  //


        if(remoteMessage.getData() != null) {
            Map<String,String> data = remoteMessage.getData();
            String title = data.get("title");
            final String message = data.get("message");

            if (title.equals("Cancel")) {

                Handler handler = new Handler(Looper.getMainLooper());
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MyFirebaseMessaging.this, message, Toast.LENGTH_SHORT).show();
                    }
                });


                LocalBroadcastManager.getInstance(MyFirebaseMessaging.this)
                        .sendBroadcast(new Intent("cancel_request"));

            } else if (title.equals("Arrived")) {
                showArrivedNotification(message);
            } else if (title.equals("DropOff")) {
                openRateActivity(message);
            }
        }

    }

// 这是来自 CustomerCall.class

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customer_call);

        btnCancel = (Button) findViewById(R.id.btnDecline);

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!TextUtils.isEmpty(customerId))
                    cancelBooking(customerId);
                finish();
            }
        });

}

private void cancelBooking(String customerId) {
        Token2 token2 = new Token2(customerId);
        Map<String,String> content = new HashMap<>();
        content.put("title","Cancel");
        content.put("message", "Driver has cancelled your request");
        DataMessage2 dataMessage2 = new DataMessage2(token2.getToken(),content);

        mFCMService.sendMessage(dataMessage2)
                .enqueue(new Callback<FCMResponse2>() {
                    @Override
                    public void onResponse(Call<FCMResponse2> call, Response<FCMResponse2> response) {
                        if (response.body().success == 1) {
                            Toast.makeText(CustomerCall.this, "Cancelled", Toast.LENGTH_SHORT).show();
                            finish();
                        }
                    }

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

标签: androidfirebase-cloud-messaging

解决方案


推荐阅读