상태표시줄 알림 만들기

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. 사용 예시

   - 해당 알림 배경을 사용자 임의로 작성할 수도 있다.