Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

java - notification spaming every time I change data

im developing a app, and every time, i change data on Firebase, the notifications multiply, cant understand why is this happening, someone expert enought to help me please. It's like, when the app starts, the value of string is null and appear 3 notification as it should be and if I change some data on firebase, appears 6 notifications and just let me use one ID to open an activity.

NotificationAgendamento.java

private static final String TAG = "notificationAgendamento";
private boolean jobCancelled;
String dataS;
LocalDate cDate, mDate, sDate, eDate;
private String data;
private Date pDate;
private NotificationManagerCompat notificationManager;


//String do corpo da notificacao
String tituloMain, tituloSecundario, corpo1, corpo2,corpo3;
String nome_pet;
String hora;
@Override
public boolean onStartJob(JobParameters params) {
    Log.d(TAG, "Job started");

    doBackGroundWork(params);

    return true;
}

@Override
public boolean onStopJob(JobParameters params) {
    Log.d(TAG, "Job cancelled before completion");
    jobCancelled = true;
    return true;
}
private void doBackGroundWork(final JobParameters params) {

    new Thread(new Runnable() {
        @Override
        public void run() {
            if (jobCancelled){
                return;
            }
            getAgendamentoNotifications();
            hora = "";
            dataS = "";
            nome_pet = "";
            Log.d(TAG, "Job finished");
            jobFinished(params, false);
        }
    }).start();

}

private void getAgendamentoNotifications() {
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

    if (user == null) {
        return;
    }

        final String userid = FirebaseAuth.getInstance().getCurrentUser().getUid();
        final DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("boletim").child(userid);
        FirebaseDatabase.getInstance().getReference().child("Utilizador").child(userid).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                String lingua = snapshot.child("lingua").getValue().toString().trim();

                switch (lingua) {
                    case "en":
                        tituloMain = "Commitment";
                        tituloSecundario = "Check Commitment";
                        corpo1 = " have a commitment scheduled for today at ";
                        corpo2 = " has an appointment scheduled for ";
                        corpo3 = " has an appointment scheduled for the week ";
                        break;
                    case "pt":
                        tituloMain = "Compromisso";
                        tituloSecundario = "Ver Compromisso";
                        corpo1 = " tem um compromisso marcado para hoje ás 
";
                        corpo2 = " tem um compromisso marcado para ";
                        corpo3 = " tem um compromisso marcado para a semana ";
                        break;
                    case "cn":
                        tituloMain = "承诺 ";
                        tituloSecundario = "查看预约 ";
                        corpo1 = " 预定今天的ace约会 
";
                        corpo2 = " 预约了 ";
                        corpo3 = " 预定一周的约会 ";
                        break;
                    case "fr":
                        tituloMain = "Engagement";
                        tituloSecundario = "Voir rendez-vous";
                        corpo1 = " avoir un engagement prévu pour aujourd'hui ";
                        corpo2 = " a un rendez-vous prévu pour ";
                        corpo3 = "  a un rendez-vous prévu pour la semaine";
                        break;
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });

        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshots) {
                for (final DataSnapshot dataSnapshotPet : snapshots.getChildren()){
                    final String key_pet = dataSnapshotPet.getKey();
                    ref.child(key_pet).addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot snapshotds) {
                            final String imagem = snapshotds.child("imagem").getValue(String.class);
                            for (DataSnapshot dataSnapshotBol : snapshotds.getChildren()){
                                String key_bol = dataSnapshotBol.getKey();
                                ref.child(key_pet).child(key_bol).addValueEventListener(new ValueEventListener() {
                                    @Override
                                    public void onDataChange(@NonNull DataSnapshot snapshotsss) {
                                        final DateFormat dateFormat = new SimpleDateFormat("HH:mm");
                                        Calendar mcurrentTime = Calendar.getInstance();
                                        int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
                                        int minute = mcurrentTime.get(Calendar.MINUTE);
                                        final String currentHour = hour + ":" + minute;
                                        final String afterHour = "08:00";
                                        hora = snapshotsss.child("hora").getValue(String.class);
                                        final String tipo = snapshotsss.child("tipo").getValue(String.class);
                                        dataS = snapshotsss.child("data").getValue(String.class);
                                        nome_pet = snapshotsss.child("nome_pet").getValue(String.class);
                                        String dtStart = dataS;
                                        DateTimeFormatter format = DateTimeFormat.forPattern("dd/MM/yyyy");
                                        mDate = org.joda.time.LocalDate.parse(dtStart, format);
                                        cDate = new LocalDate();
                                        for (int i = 0; i <= 0; i++) {
                                            final int days = Days.daysBetween(cDate, mDate).getDays();
                                            Random random = new Random();
                                            try {
                                                Bitmap bm = BitmapFactory.decodeFile(imagem);

                                                Date horaAgenda = dateFormat.parse(hora);
                                                Date currentHourDate = dateFormat.parse(currentHour);
                                                Date afterHours = dateFormat.parse(afterHour);
                                                Date beforeHours = dateFormat.parse("22:00");
                                                if (days == 0 /*&& currentHourDate.before(horaAgenda) && currentHourDate.after(afterHours)*/) {
                                                    int m = 100 + random.nextInt(9000);
                                                    notificationManager = NotificationManagerCompat.from(getApplicationContext());
                                                    Intent activityIntent = new Intent(getApplicationContext(), pet_page.class);
                                                    activityIntent.putExtra("petID", key_pet);
                                                    PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, activityIntent, 0);
                                                    Notification notification = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_1_ID)
                                                            .setSmallIcon(R.drawable.ic_dog__2)
                                                            .setContentTitle(tituloMain)
                                                            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                                                            .setAutoCancel(true)
                                                            .setOnlyAlertOnce(true)
                                                            .addAction(R.mipmap.ic_launcher, tituloSecundario, contentIntent)
                                                            .setStyle(new NotificationCompat.BigTextStyle()
                                                                    .bigText(nome_pet + corpo1 + hora))
                                                            .build();
                                                    notification.flags |= Notification.FLAG_AUTO_CANCEL;

                                                    notificationManager.notify(m, notification);
                                                } else if (days == 2 /*&& currentHourDate.before(beforeHours) && currentHourDate.after(afterHours)*/) {
                                                    int m = random.nextInt(9999 - 1000) + 1000;
                                                    notificationManager = NotificationManagerCompat.from(getApplicationContext());
                                                    Intent activityIntent = new Intent(getApplicationContext(), pet_page.class);
                                                    activityIntent.putExtra("petID", key_pet);
                                                    PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, activityIntent, 0);
                                                    Notification notification = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_1_ID)
                                                            .setSmallIcon(R.drawable.ic_dog__2)
                                                            .setContentTitle(tituloMain)
                                                            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                                                            .setAutoCancel(true)
                                                            .setOnlyAlertOnce(true)
                                                            .addAction(R.mipmap.ic_launcher, tituloSecundario, contentIntent)
                                                            .setStyle(new NotificationCompat.BigTextStyle()
                                                                    .bigText(nome_pet + corpo2 + dataS)).build();
                                                    notificationManager.notify(2, notification);
                                                } else if (days == 7 /*&& currentHourDate.before(beforeHours) && currentHourDate.after(afterHours)*/) {
                                                    int m = random.nextInt(9999 - 1000) + 1000;
                                                    notificationManager = NotificationManagerCompat.from(getApplicationContext());
                                                    Intent activityIntent = new Intent(getApplicationContext(), pet_page.class);
         

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...