1. Activity에서 Class 정의
- 구현이 단순한 관계로 설명없이 소스 서술
NotificationManager topMsgManager;
NotificationCompat.Builder topMsgBuilder;
private static String CHANNEL_ID = "test_app_topNotice";
private static String CHANEL_NAME = "test_app_topNotice";
2. 알림창을 띄울 함수 정의
public void showTopNotice() {
topMsgBuilder = null;
topMsgManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//CHANNEL_ID : 임의의 알림 id, CHANEL_NAME : 임의의 알림 이름, NotificationManager.IMPORTANCE_DEFAULT : 중요도
//IMPORTANCE_HIGH :긴급 (알림음이 울리며 헤드업 알림으로 표시), IMPORTANCE_DEFAULT : 높음 (알림음이 울림)
//IMPORTANCE_LOW : 중간 (알림음이 없음, IMPORTANCE_MIN : 낮음 (알림음이 없고 상태줄에 표시되지 않음)
//안드로이드 API 참조 : https://developer.android.com/guide/topics/ui/notifiers/notifications?hl=ko#Templates
topMsgManager.createNotificationChannel(new NotificationChannel(CHANNEL_ID, CHANEL_NAME, NotificationManager.IMPORTANCE_DEFAULT));
topMsgBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
//알림에 뜰 Icon 설정
topMsgBuilder.setSmallIcon(R.drawable.ic_launcher_foreground);
//알림 제목
topMsgBuilder.setContentTitle("Notice");
//알림에 표시할 내용 텍스트
topMsgBuilder.setContentText("알림 메시지 TEST");
//알림 Setting 데이터 빌드
Notification notification = topMsgBuilder.build();
//상단 알림 표시
topMsgManager.notify(1, notification);
}
3. 사용 예시
- 해당 알림 배경을 사용자 임의로 작성할 수도 있다.
'안드로이드 > 안드로이드_java' 카테고리의 다른 글
안드로이드 Retrofit2 적용 및 스프링 서버 데이터 통신 구축 (0) | 2022.04.08 |
---|---|
FCM Push(Firebase 기반) 알림 메시지 만들기 (0) | 2022.03.24 |
커스텀 다이얼로그 만들기 (0) | 2022.03.24 |
디스플레이 메시지(toast) 만들기 (안드로이드 10버전 이후) (0) | 2022.03.21 |
Loading 다이얼로그 만들기 (0) | 2022.03.18 |