상세 컨텐츠

본문 제목

[안드로이드]Notification 알림창에 액션기능 추가하기

안드로이드

by aries574 2020. 12. 28. 11:41

본문


2020/12/25 - [안드로이드] - [안드로이드]Notification 간단한 알림 띄우기

이번 시간에는 알림창에 액션버튼을 추가해보도록 하겠습니다.

알림내용 하단에 yes, no 라는 단어를 추가해서 클릭하면 해당 화면으로 이동하는 기능을 만드려고 합니다.


1. 액티비티 추가(YesActivity.java, NoActivity)



YesActivity.java


import androidx.appcompat.app.AppCompatActivity;
import android.app.NotificationManager;
import android.os.Bundle;

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);
}
}


NoActivity.java


import androidx.appcompat.app.AppCompatActivity;
import android.app.NotificationManager;
import android.os.Bundle;

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);
}
}


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"
tools:context=".YesActivity">

<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_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</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"
tools:context=".NoActivity">

<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>



2. 메인화면 구현(activity_main.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>


3. 메인기능 구현(MainActivity.java)

알림에 액션기능을 추가하는 방법은 addAction으로 할 수 있습니다.


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 MainActivity 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_main);

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);
}
}
}

아래 파일은 res -> drawable 폴더 안에 넣으시면 됩니다.

ic_sms.xml

4. 실행화면



반응형

관련글 더보기

댓글 영역