仅在收到第一个消息后才允许第二个消息

时间:2019-05-01 00:06:59

标签: android firebase-cloud-messaging

我正在尝试结合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(), CustommerCall.class);
            intent.putExtra("lat", lat);
            intent.putExtra("lng", lng);
            intent.putExtra("customer", customer);

            startActivity(intent);
        }

        // second message after the first one above  "CustommerCall.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 "CustommerCall.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);
            }
        }

    }

//这是来自CustommerCall.class

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custommer_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(CustommerCall.this, "Cancelled", Toast.LENGTH_SHORT).show();
                            finish();
                        }
                    }

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

0 个答案:

没有答案