0

Estoy haciendo una notificación que cuando el usuario abre la aplicación me envía una notificación en ese mismo instante, cuando se cambie el valor en un TextView notifique de nuevo pero el problema es que cuando el usuario abre la aplicación 2 o 3 veces con el mismo valor me sigue mostrando las notificaciones. Es posible notificar una sola vez hasta que se cambie de valor en un TextView?. He investigado que se puede hacer con Firebase pero quiero hacerlo en la misma aplicación, adjunto mi código .java

public void obtenerData() {
        RequestQueue requestQueue = Volley.newRequestQueue(Objects.requireNonNull(getActivity()).getApplicationContext());
        StringRequest stringRequest = new StringRequest(Request.Method.POST, DATA_URL,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response) {
                        TextView max;
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            JSONArray jsonArray = jsonObject.getJSONArray("datos");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                                textValor.setText(jsonObject1.getString("valor"));
                                if(!textValor.getText().equals("4.9")){
                                    createNotification();
                                } 
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        requestQueue.add(stringRequest);
    }

void createNotification(){
        NotificationCompat.Builder builder = new NotificationCompat.Builder(getActivity().getApplicationContext(), CHANNEL_ID);
        builder.setSmallIcon(R.drawable.ic_menu_send);
        builder.setContentTitle("Notificacion del dia");
        builder.setContentText("Valor cambiado");
        builder.setColor(Color.BLUE);
        builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
        builder.setLights(Color.MAGENTA, 1000, 1000);
        builder.setVibrate(new long[]{1000,1000,1000,1000,1000});
        builder.setDefaults(Notification.DEFAULT_SOUND);
        builder.setContentIntent(pendingIntent);
        builder.addAction(R.drawable.ic_menu_send, "VER INMEDIATAMENTE", siPendingIntent);
        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(getActivity().getApplicationContext());
        notificationManagerCompat.notify(NOTIFICACION_ID, builder.build());
    }
}

1 respuesta 1

0

Puedes usar las SharedPreferences para almacenar cuando se haya enviado la notificación (o el valor previo del TextView para comparar) y, en base a ese valor, notificar de nuevo o no.

Espero que ayude. ¡Un saludo!

1
  • Gracias por su respuesta, he estado investigando sobre SharedPreferences y parece una buena opción, estoy ahora trabajando en ella así que si me resulta útil voy a poner la solución. Saludos1
    – Etsu
    el 21 sep. 2020 a las 20:21

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.