Firebase push in textview

1

I have a query ... How can I capture a push notification in a textview or in a list ... here is my code

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    edit = (EditText) findViewById(R.id.edit);
    button = (Button) findViewById(R.id.butt);
   send = (Button) findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            token = FirebaseInstanceId.getInstance().getToken();
            edit.setText(token);
        }
    });
    send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            GetDataFromEditText();
            SendData(data);
        }
    });

}
public void GetDataFromEditText(){
        data = edit.getText().toString();
        Toast.makeText(MainActivity.this,data,Toast.LENGTH_LONG).show();
    }
public void SendData(final String token) {
    class PostData extends AsyncTask<String,Void,String>{
        @Override
        protected String doInBackground(String... params) {
            String tokenHoldre = token;
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
           // nameValuePairs.add(new BasicNameValuePair("tokenfirebase", tokenHoldre));
            nameValuePairs.add(new BasicNameValuePair("fcm_token", tokenHoldre));
          //  nameValuePairs.add(new BasicNameValuePair("idcliente", "08366"));
        //    nameValuePairs.add(new BasicNameValuePair("esregistro", "1"));
            try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(ServerURL);
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
            } catch (ClientProtocolException e) {

            } catch (IOException e) {

            }
            return "Success..";
        }
        protected void onPostExecute(String result) {

            super.onPostExecute(result);

            Toast.makeText(MainActivity.this, "Success..", Toast.LENGTH_LONG).show();
        }
    }
    PostData sendPostReqAsyncTask = new PostData();
   sendPostReqAsyncTask.execute(token);
}

Here is my callback:

   @Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    if(remoteMessage.getData().size() >0){
        type="json";
        sendNotificatio(remoteMessage.getData().toString());
    }
    if(remoteMessage.getNotification() !=null) {
        type = "message";
        sendNotificatio(remoteMessage.getNotification().getBody());

    }
}

private void sendNotificatio(String messageBody) {
    String id="";
    String message="";
    String titles="";

    if (type.equals("json")){
        try {
            JSONObject jsonObject = new JSONObject(messageBody);
            id=jsonObject.getString("id");
            message=jsonObject.getString("message");
            titles= jsonObject.getString("title");

        } catch (JSONException e) {
            e.printStackTrace();
        }

    } else if (type.equals("message")) {
        message= messageBody;
    }
    
asked by Wid Maer 08.08.2017 в 18:34
source

1 answer

2

I briefly comment on the reception of data through FCM in your code. When a Push notification is sent from the server, the onMessageReceived() method receives the data in .json format,

  @Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    if(remoteMessage.getData().size() >0){
        type="json";
        sendNotificatio(remoteMessage.getData().toString());
    }
    if(remoteMessage.getNotification() !=null) {
        type = "message";
        sendNotificatio(remoteMessage.getNotification().getBody());

    }
}

to later be sent to the sendNotificatio () method, this is where the JSONObject is processed and the data is obtained, you get id, message and titles, you can create a message and call a method to assign the text to your TextView , for example:

private void sendNotificatio(String messageBody) {
    String id="";
    String message="";
    String titles="";

    if (type.equals("json")){
        try {
            JSONObject jsonObject = new JSONObject(messageBody);
            id=jsonObject.getString("id");
            message=jsonObject.getString("message");
            titles= jsonObject.getString("title");

         //*** Metodo para modificar
         setTexto("id: " + id + ", message: " + message + ", titles: " + titles);

        } catch (JSONException e) {
            e.printStackTrace();
        }

    } else if (type.equals("message")) {
        message= messageBody;
    }

The method that would make the modification of the text within TextView would be for example:

public static void setTexto(String mensaje){
   textView.setText(mensaje);
}

If you want to send the data between Activities you can use the bundle to send the data using a Intent :

Passing data between activities

    
answered by 09.08.2017 в 04:57