Android Studio) 알림(Notification)

chaeyoung·2023년 1월 19일
0

Android Studio

목록 보기
11/13
post-thumbnail

알림의 기본 구성

  • 알림(Notification): 앱의 각종 상황을 사용자에게 알릴 목적으로 이용하는 기능
  • 서비스 컴포넌트, 브로드캐스트 리시버에서 주로 사용
  • support 라이브러리에서 NotificationCompat 클래스 제공
  • 클래스기능
    NotificationManager알람 시스템을 발생시키는 SystemService
    Notification알람 구성 정보를 가지는 객체
    NotificationCompat.Builder알람을 다양한 정보로 생성
    NotificationChannel알람의 관리 단위

NotificationChannel

  • Notification 객체는 직접 생성되지 않는다
    • NotificationChannel에 의해 Builder가 생성된다.
      -NotificationChannel: 알람에 대한 관리 단위
  • 버전 분기 프로그램 작성하여 채널을 적용한 알림을 발생시키는 방법
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.0){
    	String channelId = "one-channel";
        String channelName = "My channel One";
        String channelDescription = "My channel One Description";
        NotificationChannel channel = new NotificationChannel(channelId, channelName, 
        			NotificationManager.IMPORTANCE_DEFFAULT);
        channel.setDescription(channelDescription);
        
        // 각종 채널 설정
        channel.enableLight(true);
        channel.setLightColor(Color.RED);
        channel.enableVibration(true);
        ....
        
        // channel에 등록된 builder
        builder = new NotificationCompat.Builder(this, channelId);
    } else{
    	builder = new NotificationCompat.Builder(this);
    }

기본적인 알림 구성

  • Builder의 각종 setter 함수 이용해 알람의 구성 정보 명시

    함수
    setSmallIcon작은 아이콘 이미지 저장
    setWhen시간
    setContentTitle확장 내용 타이틀 문자열
    setContentText확장 내용의 본문 문자열
    setDefaultsDEFULT_SOUND, VIBRATE, LIGHTS 함께 지정가능
    setAutoCancel터치시 자동 삭제 여부, true 값이 지정되면 터치시 삭제
    setOngoing진행표시여부, true 설정시 사용자가 손가락으로 밀어서 삭제불가

알림의 다양한 구성

간단한 이벤트처리: Action

  • 화면 전환 없이 간단히 알림에 사용자의 추가 이벤트 처리하는 액션제작
    builder.addAction(new NotificationCompat.Action.Builder(android.R.drawable.ic_menu_share, 
    															"ACTION!", pIntent1).build());
    • 추가 이벤트는 addAction 함수로 설정, 내용 구성은 Compat.Action~ 로 제작

목록: InboxStyle

  • 알람에 여러 데이터를 목록 형태로 제공

    NotificationCompat.InboxStyle style = new NotificationCompat.InBoxStyle(builder);
    
    style.addLine("activity")
    style.addLine("service")
    style.setSummaryText("Android Component");
    builder.setStyle(style);

프로그레스: Progress

  • 작업의 진행 사항 표시(ex. 다운로드 현황,음악..)
    • Builder의 setProgress()함수에 progress 값 대입
    • 알림을 없앨 수 없게할때: setAutiCancel() -> false, setOngoing() -> true로 설정

상단에 띄우기: Heads Up

  • 알람 상단에 고정 혹은 중요한 내용 가장 위에 뜨기
    NotificationChannel channel = new NotificationChannel(channeId, 
    channelName, NotificationManager.IMPORTANCE_HIGH);

Person

  • 사람에 대한 정보 표현(카톡온 사람의 이름, 프사)

    Person sender1 = new Person.Builder()
    		.setName("kkang")
          .setIcon(Icon.createWithResource(this, R.drawable.person1))
          .build();
    ...
    

0개의 댓글