Komang Putra Ответов: 0

Показывать уведомление один раз при автоматическом обновлении


Мне нужна помощь, я хочу показать уведомление один раз, когда автоматическое обновление..
вот мой код

для освежения
public void content() {
        getDetail();
        refresh(1000);
    }

    private void refresh(int milliseconds) {
        final Handler handler =new Handler();
        final Runnable runnable = new Runnable() {
            @Override
            public void run() {
                content();
            }
        };
        handler.postDelayed(runnable, milliseconds);
    }


getDetail()
private void getDetail() {
        String lokasi = text_jabatan.getText().toString();
        System.out.println("Test1 :" + lokasi);

        StringRequest stringRequest = new StringRequest(Request.Method.GET, "http://example/rest_server/pengajuan/izin_full_day/index_atasan?jabatan_struktur=" + lokasi,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject obj = new JSONObject(response);
                            JSONArray movieArray = obj.getJSONArray("data");
                            int number1 = 0;

                            for (int i = 0; i < movieArray.length(); i++) {

                                JSONObject movieObject = movieArray.getJSONObject(i);

                                approvalfulldaymodel movieItem = new approvalfulldaymodel(
                                        movieObject.getString("id_full_day"),
                                        movieObject.getString("no_pengajuan_full_day"),
                                        movieObject.getString("tanggal_pengajuan"),
                                        movieObject.getString("lokasi_struktur"),
                                        movieObject.getString("nama_karyawan_struktur"),
                                        movieObject.getString("jenis_full_day"),
                                        movieObject.getString("start_full_day"),
                                        movieObject.getString("karyawan_pengganti"),
                                        movieObject.getString("ket_tambahan"),
                                        movieObject.getString("status_full_day"),
                                        movieObject.getString("feedback_full_day"));

                                if (movieObject.getString("status_full_day").contains("0") && movieObject.getString("lokasi_struktur").equalsIgnoreCase(txt_lokasi.getText().toString()))
                                    number1++;
                                {
                                    izincount.setText(String.valueOf(number1));
                                }
                                if ("1".equalsIgnoreCase(txt_alpha.getText().toString())){
                                    jumlahizin.setVisibility(View.INVISIBLE);
                                    jumlahdinas.setVisibility(View.INVISIBLE);
                                    jumlahcuti.setVisibility(View.INVISIBLE);
                                } else if ("2".equalsIgnoreCase(txt_alpha.getText().toString())){
                                    jumlahizin.setVisibility(View.INVISIBLE);
                                    jumlahdinas.setVisibility(View.INVISIBLE);
                                    jumlahcuti.setVisibility(View.INVISIBLE);
                                } else if ("3".equalsIgnoreCase(txt_alpha.getText().toString())){
                                    jumlahizin.setVisibility(View.INVISIBLE);
                                    jumlahdinas.setVisibility(View.INVISIBLE);
                                    jumlahcuti.setVisibility(View.INVISIBLE);
                                }
                                movieItemList1.add(movieItem);


                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    }
                });

        StringRequest stringRequest2 = new StringRequest(Request.Method.GET, "http://example/rest_server/pengajuan/izin_non_full_day/index_atasan?jabatan_struktur=" + lokasi,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject obj = new JSONObject(response);
                            JSONArray movieArray = obj.getJSONArray("data");
                            int number = 0;

                            for (int i = 0; i < movieArray.length(); i++) {

                                JSONObject movieObject = movieArray.getJSONObject(i);

                                approvalnonfullmodel movieItem = new approvalnonfullmodel(
                                        movieObject.getString("id_non_full"),
                                        movieObject.getString("no_pengajuan_non_full"),
                                        movieObject.getString("tanggal_pengajuan"),
                                        movieObject.getString("lokasi_struktur"),
                                        movieObject.getString("nama_karyawan_struktur"),
                                        movieObject.getString("jenis_non_full"),
                                        movieObject.getString("tanggal_non_full"),
                                        movieObject.getString("ket_tambahan_non_full"),
                                        movieObject.getString("status_non_full"),
                                        movieObject.getString("feedback_non_full"));

                                if (movieObject.getString("status_non_full").contains("0") && movieObject.getString("lokasi_struktur").equalsIgnoreCase(txt_lokasi.getText().toString()))
                                    number++;
                                {
                                    izinnoncount.setText(String.valueOf(number));
                                }
                                if ("1".equalsIgnoreCase(txt_alpha.getText().toString())){
                                    jumlahizin.setVisibility(View.INVISIBLE);
                                    jumlahdinas.setVisibility(View.INVISIBLE);
                                    jumlahcuti.setVisibility(View.INVISIBLE);
                                } else if ("2".equalsIgnoreCase(txt_alpha.getText().toString())){
                                    jumlahizin.setVisibility(View.INVISIBLE);
                                    jumlahdinas.setVisibility(View.INVISIBLE);
                                    jumlahcuti.setVisibility(View.INVISIBLE);
                                } else if ("3".equalsIgnoreCase(txt_alpha.getText().toString())){
                                    jumlahizin.setVisibility(View.INVISIBLE);
                                    jumlahdinas.setVisibility(View.INVISIBLE);
                                    jumlahcuti.setVisibility(View.INVISIBLE);
                                }
                                movieItemList2.add(movieItem);
                                count();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    }
                });

        StringRequest stringRequest3 = new StringRequest(Request.Method.GET, "http://example/rest_server/pengajuan/dinas_full_day/index_atasan?jabatan_struktur="+ lokasi,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject obj = new JSONObject(response);
                            JSONArray movieArray = obj.getJSONArray("data");
                            int number3 = 0;

                            for (int i = 0; i < movieArray.length(); i++) {

                                JSONObject movieObject = movieArray.getJSONObject(i);

                                dinasfulldaymodel movieItem = new dinasfulldaymodel(
                                        movieObject.getString("tanggal_pengajuan"),
                                        movieObject.getString("no_pengajuan_full_day"),
                                        movieObject.getString("jenis_full_day"),
                                        movieObject.getString("start_full_day"),
                                        movieObject.getString("ket_tambahan"),
                                        movieObject.getString("lokasi_struktur"),
                                        movieObject.getString("status_full_day"),
                                        movieObject.getString("tanggal_approval"),
                                        movieObject.getString("feedback_full_day"),
                                        movieObject.getString("status_full_day_2"),
                                        movieObject.getString("tanggal_approval_2"),
                                        movieObject.getString("feedback_full_day_2"));


                                if (movieObject.getString("status_full_day").contains("0") && movieObject.getString("lokasi_struktur").equalsIgnoreCase(txt_lokasi.getText().toString()))
                                    number3++;  {
                                    dinascount.setText(String.valueOf(number3));
                                }
                                if ("1".equalsIgnoreCase(txt_alpha.getText().toString())){
                                    jumlahizin.setVisibility(View.INVISIBLE);
                                    jumlahdinas.setVisibility(View.INVISIBLE);
                                    jumlahcuti.setVisibility(View.INVISIBLE);
                                } else if ("2".equalsIgnoreCase(txt_alpha.getText().toString())){
                                    jumlahizin.setVisibility(View.INVISIBLE);
                                    jumlahdinas.setVisibility(View.INVISIBLE);
                                    jumlahcuti.setVisibility(View.INVISIBLE);
                                } else if ("3".equalsIgnoreCase(txt_alpha.getText().toString())){
                                    jumlahizin.setVisibility(View.INVISIBLE);
                                    jumlahdinas.setVisibility(View.INVISIBLE);
                                    jumlahcuti.setVisibility(View.INVISIBLE);
                                }

                                movieItemList3.add(movieItem);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    }
                });

        StringRequest stringRequest4 = new StringRequest(Request.Method.GET, "http://example/rest_server/pengajuan/dinas_non_full_day/index_atasan?jabatan_struktur="+ lokasi,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject obj = new JSONObject(response);
                            JSONArray movieArray = obj.getJSONArray("data");
                            int number4 = 0;

                            for (int i = 0; i < movieArray.length(); i++) {

                                JSONObject movieObject = movieArray.getJSONObject(i);

                                final approvaldinasnonfullmodel movieItem = new approvaldinasnonfullmodel(
                                        movieObject.getString("id_non_full"),
                                        movieObject.getString("tanggal_pengajuan"),
                                        movieObject.getString("nama_karyawan_struktur"),
                                        movieObject.getString("lokasi_struktur"),
                                        movieObject.getString("jenis_non_full"),
                                        movieObject.getString("tanggal_non_full"),
                                        movieObject.getString("ket_tambahan_non_full"),
                                        movieObject.getString("status_non_full"),
                                        movieObject.getString("feedback_non_full"));

                                if (movieObject.getString("status_non_full").contains("0") && movieObject.getString("lokasi_struktur").equalsIgnoreCase(txt_lokasi.getText().toString()))
                                    number4++;
                                {
                                    dinasnoncount.setText(String.valueOf(number4));
                                }
                                if ("1".equalsIgnoreCase(txt_alpha.getText().toString())) {
                                    jumlahizin.setVisibility(View.INVISIBLE);
                                    jumlahdinas.setVisibility(View.INVISIBLE);
                                    jumlahcuti.setVisibility(View.INVISIBLE);
                                    jumlahizin.setVisibility(View.INVISIBLE);
                                } else if ("2".equalsIgnoreCase(txt_alpha.getText().toString())) {
                                    jumlahdinas.setVisibility(View.INVISIBLE);
                                    jumlahcuti.setVisibility(View.INVISIBLE);
                                } else if ("3".equalsIgnoreCase(txt_alpha.getText().toString())) {
                                    jumlahizin.setVisibility(View.INVISIBLE);
                                    jumlahdinas.setVisibility(View.INVISIBLE);
                                    jumlahcuti.setVisibility(View.INVISIBLE);
                                }

                                movieItemList4.add(movieItem);
                                count2();
                            }


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    }
                });

        StringRequest stringRequest5 = new StringRequest(Request.Method.GET, "http://example/rest_server/pengajuan/cuti_tahunan/index_atasan?jabatan_struktur="+ lokasi,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject obj = new JSONObject(response);
                            JSONArray movieArray = obj.getJSONArray("data");
                            int number5 = 0;

                            for (int i = 0; i < movieArray.length(); i++) {

                                JSONObject movieObject = movieArray.getJSONObject(i);

                                approvalcutitahunan movieItem = new approvalcutitahunan(
                                        movieObject.getString("id_sisa_cuti"),
                                        movieObject.getString("tanggal_pengajuan"),
                                        movieObject.getString("nama_karyawan_struktur"),
                                        movieObject.getString("start_cuti_tahunan"),
                                        movieObject.getString("lokasi_struktur"),
                                        movieObject.getString("ket_tambahan_tahunan"),
                                        movieObject.getString("opsi_cuti_tahunan"),
                                        movieObject.getString("status_cuti_tahunan"),
                                        movieObject.getString("feedback_cuti_tahunan"));


                                if (movieObject.getString("status_cuti_tahunan").contains("0") && movieObject.getString("lokasi_struktur").equalsIgnoreCase(txt_lokasi.getText().toString()))
                                    number5++;  {
                                    cutitahunancount.setText(String.valueOf(number5));
                                }
                                if ("1".equalsIgnoreCase(txt_alpha.getText().toString())){
                                    jumlahizin.setVisibility(View.INVISIBLE);
                                    jumlahdinas.setVisibility(View.INVISIBLE);
                                    jumlahcuti.setVisibility(View.INVISIBLE);
                                } else if ("2".equalsIgnoreCase(txt_alpha.getText().toString())){
                                    jumlahizin.setVisibility(View.INVISIBLE);
                                    jumlahdinas.setVisibility(View.INVISIBLE);
                                    jumlahcuti.setVisibility(View.INVISIBLE);
                                } else if ("3".equalsIgnoreCase(txt_alpha.getText().toString())){
                                    jumlahizin.setVisibility(View.INVISIBLE);
                                    jumlahdinas.setVisibility(View.INVISIBLE);
                                    jumlahcuti.setVisibility(View.INVISIBLE);
                                }

                                movieItemList5.add(movieItem);

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    }
                });

        StringRequest stringRequest6 = new StringRequest(Request.Method.GET, "http://example/rest_server/pengajuan/cuti_khusus/index_atasan?jabatan_struktur="+ lokasi,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject obj = new JSONObject(response);
                            JSONArray movieArray = obj.getJSONArray("data");
                            int number = 0;

                            for (int i = 0; i < movieArray.length(); i++) {

                                JSONObject movieObject = movieArray.getJSONObject(i);

                                approvalcutikhusus movieItem = new approvalcutikhusus(
                                        movieObject.getString("id_cuti_khusus"),
                                        movieObject.getString("tanggal_pengajuan"),
                                        movieObject.getString("nama_karyawan_struktur"),
                                        movieObject.getString("start_cuti_khusus"),
                                        movieObject.getString("lokasi_struktur"),
                                        movieObject.getString("ket_tambahan_khusus"),
                                        movieObject.getString("jenis_cuti_khusus"),
                                        movieObject.getString("status_cuti_khusus"),
                                        movieObject.getString("feedback_cuti_khusus"));


                                if (movieObject.getString("status_cuti_khusus").contains("0") && movieObject.getString("lokasi_struktur").equalsIgnoreCase(txt_lokasi.getText().toString()))
                                    number++;  {
                                    cutikhususcount.setText(String.valueOf(number));
                                }
                                if ("1".equalsIgnoreCase(txt_alpha.getText().toString())){
                                    jumlahizin.setVisibility(View.INVISIBLE);
                                    jumlahdinas.setVisibility(View.INVISIBLE);
                                    jumlahcuti.setVisibility(View.INVISIBLE);
                                } else if ("2".equalsIgnoreCase(txt_alpha.getText().toString())){
                                    jumlahizin.setVisibility(View.INVISIBLE);
                                    jumlahdinas.setVisibility(View.INVISIBLE);
                                    jumlahcuti.setVisibility(View.INVISIBLE);
                                } else if ("3".equalsIgnoreCase(txt_alpha.getText().toString())){
                                    jumlahizin.setVisibility(View.INVISIBLE);
                                    jumlahdinas.setVisibility(View.INVISIBLE);
                                    jumlahcuti.setVisibility(View.INVISIBLE);

                                }

                                movieItemList6.add(movieItem);
                              count3();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    }
                });

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
        requestQueue.add(stringRequest2);
        requestQueue.add(stringRequest3);
        requestQueue.add(stringRequest4);
        requestQueue.add(stringRequest5);
        requestQueue.add(stringRequest6);

    }

для показа уведомления
private void showNotif() {
        String NOTIFICATION_CHANNEL_ID = "channel_androidnotif";
        Context context = this.getApplicationContext();
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            String channelName = "Android Notif Channel";
            int importance = NotificationManager.IMPORTANCE_HIGH;

            NotificationChannel mChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, importance);
            notificationManager.createNotificationChannel(mChannel);
        }

        Intent mIntent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("fromnotif", "notif");
        mIntent.putExtras(bundle);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);
        builder.setContentIntent(pendingIntent)
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_launcher_background))
                .setTicker("notif starting")
                .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
                .setLights(Color.RED, 3000, 3000)
                .setDefaults(Notification.DEFAULT_SOUND)
                .setOnlyAlertOnce(true)
                .setContentTitle("Semangat Pagi")
                .setContentText("Ada beberapa approval yang sedang menunggu untuk di approve");

        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.cancel(100);
        notificationManager.notify(0 , builder.build());
    }


условие для отображения диалогового окна...
private void count(){
        int num1 = (int) Double.parseDouble(izinnoncount.getText().toString());
        int num2 = (int) Double.parseDouble(izincount.getText().toString());
        int sum = num1 + num2;

        jumlahizin.setText(Integer.toString(sum));
        if (1 <= sum) {
            if ("4".equalsIgnoreCase(txt_alpha.getText().toString()) ||
                    ("5".equalsIgnoreCase(txt_alpha.getText().toString())||
                            ("6".equalsIgnoreCase(txt_alpha.getText().toString()) ||
                                    ("7".equalsIgnoreCase(txt_alpha.getText().toString()) ||
                                            ("8".equalsIgnoreCase(txt_alpha.getText().toString()) ||
                                                    ("9".equalsIgnoreCase(txt_alpha.getText().toString()) ||
                                                            ("10".equalsIgnoreCase(txt_alpha.getText().toString())))))))) {
                showNotif();
            }
        }
    }
    private void count2(){
        int num3 = (int) Double.parseDouble(dinascount.getText().toString());
        int num4 = (int) Double.parseDouble(dinasnoncount.getText().toString());
        int sum2 = num3 + num4;
        jumlahdinas.setText(Integer.toString(sum2));
        if (1 <= sum2) {
            if ("4".equalsIgnoreCase(txt_alpha.getText().toString()) ||
                    ("5".equalsIgnoreCase(txt_alpha.getText().toString()) ||
                            ("6".equalsIgnoreCase(txt_alpha.getText().toString()) ||
                                    ("7".equalsIgnoreCase(txt_alpha.getText().toString()) ||
                                            ("8".equalsIgnoreCase(txt_alpha.getText().toString()) ||
                                                    ("9".equalsIgnoreCase(txt_alpha.getText().toString()) ||
                                                            ("10".equalsIgnoreCase(txt_alpha.getText().toString())))))))) {
                showNotif();
            }
        }
    }
    private  void count3(){
        int num5 = (int) Double.parseDouble(cutikhususcount.getText().toString());
        int num6 = (int) Double.parseDouble(cutitahunancount.getText().toString());
        int sum3 = num5 + num6;
        jumlahcuti.setText(Integer.toString(sum3));
        if (1 <= sum3) {
            if ("4".equalsIgnoreCase(txt_alpha.getText().toString()) ||
                    ("5".equalsIgnoreCase(txt_alpha.getText().toString()) ||
                            ("6".equalsIgnoreCase(txt_alpha.getText().toString()) ||
                                    ("7".equalsIgnoreCase(txt_alpha.getText().toString()) ||
                                            ("8".equalsIgnoreCase(txt_alpha.getText().toString()) ||
                                                    ("9".equalsIgnoreCase(txt_alpha.getText().toString()) ||
                                                            ("10".equalsIgnoreCase(txt_alpha.getText().toString())))))))) {
                showNotif();
            }
        }
    }


Что я уже пробовал:

в этом случае, когда я удаляю уведомление, оно продолжает появляться каждый раз...

Richard MacCutchan

Пожалуйста, удалите код, который не связан с вашим вопросом, и объясните, где именно возникает проблема.

David Crow

Это чрезвычайно трудно читать, не говоря уже о неэффективности:

if (1 <= sum3) {
            if ("4".equalsIgnoreCase(txt_alpha.getText().toString()) ||
                    ("5".equalsIgnoreCase(txt_alpha.getText().toString()) ||
                            ("6".equalsIgnoreCase(txt_alpha.getText().toString()) ||
                                    ("7".equalsIgnoreCase(txt_alpha.getText().toString()) ||
                                            ("8".equalsIgnoreCase(txt_alpha.getText().toString()) ||
                                                    ("9".equalsIgnoreCase(txt_alpha.getText().toString()) ||
                                                            ("10".equalsIgnoreCase(txt_alpha.getText().toString())))))))) {
                showNotif();
            }
        }
Как насчёт:
if (1 <= sum3) 
{
    int alpha = Integer.parseInt(txt_alpha.getText().toString());
    if (4 <= alpha && alpha <= 10)
        showNotif();
}

А теперь иди и сделай то, что предложил Ричард.

0 Ответов