안드로이드

[Android] 노티피케이션(알림)과 펜딩인텐트

IT꿈나무 2021. 5. 26. 18:24
반응형

 

        Intent intent = new Intent(this, SubActivity.class);
        //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); //1회만 사용되는 펜딩인텐트

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "CHANNEL_ID")
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setContentTitle("My notification")
                .setContentText("Hello World!")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                //.setOngoing(true) // 사용자가 직접 못지우게 계속 실행하기.
                // Set the intent that will fire when the user taps the notification
                .setContentIntent(pendingIntent)
                .setAutoCancel(true);


        btnTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NotificationManagerCompat notificationManager = NotificationManagerCompat.from(MainActivity.this);
                // notificationId is a unique int for each notification that you must define
                notificationManager.notify(0, builder.build()); //노티피케이션 실행

                Toast.makeText(getApplicationContext(),"헐헐헐",Toast.LENGTH_SHORT).show();
//                try {
//                    pendingIntent.send(); //
//                } catch (PendingIntent.CanceledException e) {
//                    e.printStackTrace();
//                }

            }
        });

 

참고 자료:
[1] 알림 만들기, https://developer.android.com/training/notify-user/build-notification?hl=ko

반응형