[JAVA] Add Button in Notification

2021. 2. 20. 18:36Android

Notification Add Option

 

Notification에 Button을 Add 하는 방법

 

1. Notifcation을 눌렀을때 일반적으로 반응할 Intent 선언

2. Notification에서 Button을 눌렀을떄 사용할 Intent를 선언

3. Notification에서 addAction을 추가 (Notifcation EXIT 기능 Action)

        createNotificationChannel();
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, 0);

        //end option
        Intent notificationcancleIntent = new Intent(this, MainActivity.class);
        //stop userIntent input
        notificationcancleIntent.setAction("Stop");
        PendingIntent canclependingIntent = PendingIntent.getActivity(this,-99,notificationcancleIntent,PendingIntent.FLAG_UPDATE_CURRENT);


        Notification notification = new NotificationCompat.Builder(this, TAG)
                .setContentTitle("Foreground Service")
                .setContentText(input)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentIntent(pendingIntent)
                .setStyle(new NotificationCompat.BigTextStyle().bigText("Foreground Service App"))
                .addAction(R.drawable.ic_launcher_foreground,"EXIT", canclependingIntent)
                .setAutoCancel(true)
                .build();

 

FULL Code

 

github.com/Bsoyoung/fg_SMSReceiver/tree/a18f58a904847d7a55b4c9db5a1676fb5891ea6e

 

Bsoyoung/fg_SMSReceiver

initial commit(foreground service). Contribute to Bsoyoung/fg_SMSReceiver development by creating an account on GitHub.

github.com