이번 시간에는 간단한 알림 메시지를 띄우는 앱을 만들어 보겠습니다.
사용자가 직접 제목과 내용을 입력하고 버튼을 누르면 알림이 띄어집니다.
1. 알림 기능 구현(NotificationHelper.java)
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.os.Build;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
public class NotificationHelper extends ContextWrapper {
public static final String channel1ID = "channel1ID";
public static final String channel1Name = "channel 1";
private NotificationManager mManager;
public NotificationHelper(Context base) {
super(base);
//오레오보다 같거나 크면
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
createChannels();
}
}
//채널 생성
@RequiresApi(api = Build.VERSION_CODES.O)
public void createChannels(){
NotificationChannel channel1 = new NotificationChannel(channel1ID, channel1Name, NotificationManager.IMPORTANCE_DEFAULT);
channel1.enableLights(true);
channel1.enableVibration(true);
channel1.setLightColor(R.color.colorPrimary);
channel1.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
getManager().createNotificationChannel(channel1);
}
public NotificationManager getManager(){
if(mManager == null){
mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return mManager;
}
public NotificationCompat.Builder getChannel1Notification(String title, String message){
return new NotificationCompat.Builder(getApplicationContext(), channel1ID)
.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.drawable.ic_launcher_background);
}
}
2. 메인화면 구현(activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/edit_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Title"
/>
<EditText
android:id="@+id/edit_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="message"
/>
<Button
android:id="@+id/btn_Channel1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="알림 띄우기" />
</LinearLayout>
3. 메인기능 구현(MainActivity.java)
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText eTitle;
private EditText eMessage;
private Button channel1Btn;
private NotificationHelper mNotificationhelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
eTitle = findViewById(R.id.edit_title);
eMessage = findViewById(R.id.edit_message);
channel1Btn = findViewById(R.id.btn_Channel1);
mNotificationhelper = new NotificationHelper(this);
channel1Btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String title = eTitle.getText().toString();
String message = eMessage.getText().toString();
sendOnChannel1(title, message);
}
});
}
public void sendOnChannel1(String title, String message){
NotificationCompat.Builder nb = mNotificationhelper.getChannel1Notification(title, message);
mNotificationhelper.getManager().notify(1, nb.build());
}
}
4. 실행화면
2020/12/24 - [안드로이드] - [안드로이드] DatePicker 달력(캘린더) 만들어보기
맘에 드셨다면 공감 부탁드려요문의 댓글 환영합니다. |
[안드로이드] Notification 알림창에 댓글(Reply) 기능 추가 (0) | 2020.12.29 |
---|---|
[안드로이드]Notification 알림창에 액션기능 추가하기 (2) | 2020.12.28 |
[안드로이드]DatePicker 달력(캘린더) 만들어보기 (0) | 2020.12.24 |
[안드로이드]TimePicker 내가 선택한 알람시간 텍스트뷰에 보여주기 (0) | 2020.12.23 |
[안드로이드]버튼 색상 모양 코드에서 변경하는 방법 (0) | 2020.12.22 |
댓글 영역