728x90
안녕하세요 !
오늘은 notification 알림창에 액션 버튼을 생성해보겠습니다.
먼저 xml 파일들을 만들어줍니다.
activity_noti_action.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="알림 호출"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
notification을 호출할 수 있는 버튼이 있는 화면 입니다.
activity_yes.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yes Activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.469" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="메인으로"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.516" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_no.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No Activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
drawble -> ic_sms.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M20,2L4,2c-1.1,0 -1.99,0.9 -1.99,2L2,22l4,-4h14c1.1,0 2,-0.9 2,-2L22,4c0,-1.1 -0.9,-2 -2,-2zM9,11L7,11L7,9h2v2zM13,11h-2L11,9h2v2zM17,11h-2L15,9h2v2z"/>
</vector>
drawble 파일에 해당 아이콘을 만들어줍니다.
다음으로 자바 파일들을 만들어줍니다.
NotiActionActivity.java
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class NotiActionActivity extends AppCompatActivity {
public final String CHANNEL_ID = "my_notification_channel";
public final int NOTIFICATION_ID = 101;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_noti_action);
Button notificationBtn = findViewById(R.id.button);
notificationBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
displayNotification(v);
}
});
}
//알림 설정
public void displayNotification(View v){
createNotificationChannel();
//MainActivity 설정
Intent mainIntent = new Intent(this, MainActivity.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent MainPendingIntent = PendingIntent.getActivity(this , 0, mainIntent, PendingIntent.FLAG_ONE_SHOT);
//YesActivity 설정
Intent yesIntent = new Intent(this, YesActivity.class);
yesIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent yesPendingIntent = PendingIntent.getActivity(this, 0, yesIntent, PendingIntent.FLAG_ONE_SHOT);
//NoActivity 설정
Intent noIntent = new Intent(this, NoActivity.class);
noIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent noPendingIntent = PendingIntent.getActivity(this, 0, noIntent, PendingIntent.FLAG_ONE_SHOT);
//알림설정
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setSmallIcon(R.drawable.ic_sms);
builder.setContentTitle("알림예제");
builder.setContentText("알림이 왔습니다.");
builder.setPriority(NotificationManagerCompat.IMPORTANCE_DEFAULT);
builder.setAutoCancel(true);
builder.setContentIntent(MainPendingIntent);
builder.addAction(R.drawable.ic_sms, "Yes", yesPendingIntent);
builder.addAction(R.drawable.ic_sms, "No", noPendingIntent);
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
notificationManagerCompat.notify(NOTIFICATION_ID, builder.build());
}
//채널 셋팅
private void createNotificationChannel(){
//오레오부터는 알림을 채널에 등록해야 한다.
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
CharSequence name = "채널이름";
String description = "채널설명";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
//채널생성
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, name, importance);
notificationChannel.setDescription(description);
//알림매니저 생성
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//알림매니저에 채널등록록
notificationManager.createNotificationChannel(notificationChannel);
}
}
}
YesActivity.java
import android.app.NotificationManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class YesActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yes);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(101);
Button notificationBtn = findViewById(R.id.button);
notificationBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), NotiActionActivity.class);
startActivity((intent));
}
});
}
}
NoActivity.java
import android.app.NotificationManager;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class NoActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_no);
//알림창 없애기
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(101);
}
}
다음으로 실행화면 입니다.
보시는 것과 같이 노티피케이션에 버튼이 추가되고, 각 버튼에 따라 다른 기능이 동작하는 것을 확인할 수 있습니다.
오늘 준비한 내용은 여기까지입니다.
궁금한 점은 언제든 댓글로 남겨주세요.
감사합니다 !
'안드로이드 자바' 카테고리의 다른 글
[Android][Java] 네이버 map api - 커스텀 마커와 마커 클릭 이벤트 다루기 (0) | 2023.07.01 |
---|---|
[Android][JAVA] TextToSpeech를 사용해 텍스트(Text)를 음성으로 바꾸기 (0) | 2023.06.26 |
[Android][JAVA] 입력한 내용을 텍스트 파일에 저장하기 (0) | 2023.06.22 |
[Android][JAVA] 이미지 투명 애니메이션 구현하기 (0) | 2023.06.21 |
[Android][JAVA] Youtube API를 이용하지 않고 유튜브 영상 재생하기 (0) | 2023.06.20 |