프로그래밍/Android

상태바 알림 처리

chance 2009. 11. 9. 00:40

 

상태바 알림

 

노티피케이션은 노티피케이션 객체와 관리자를 사용하게 된다.

노티피케이션 객체도 컨텍스트 객체에서 얻어올 수 있다.

이전에 포스팅한  alert, toast 같이 Context getSystemService() 사용한다.

 

 

1. Notification 매니저 얻기 : 아래처럼 얻을 수도 있다.

String ns = Context.NOTIFICATION_SERVICE;

NotificationManger notiManager = (NotificationManager) getSystemService( ns );

 

 

2. Notification 객체 생성

상태바 아이콘, 티커메시지, 발생 시간으로 구성되어 있다.

CharSequence ticker = "Test";

Notification notification =

new Notification( R.drawable.noti_icon, ticker, System.currentTimeMillis() );

 

3. notification id 설정

private static final int NOTI_ID = 1;

 

 

4. 알림 이후 사용자가 해당 알림을 선택하면

노티피케이션 리스트 윈도우가 뜨는데, 해당 윈도우에 표시될 내용을 설정한다.

또한, 해당 항목이 처리할 Intent를 추가 한다.

 

Context context = getApplicationContext();

CharSequence contentTitle="Title";

CharSequence contentText="Hello";

Intent notificationIntent = new Intent( this, MyClass.class );

PendingIntent contentIntent 

= PendingIntent.getActivity( Context context, int reqCode, Intent intent, int flags );

 

notification.setLatestEventInfo( context, contentTitle, contentText, contentIntent );

 

5. notification 전달

notiManager.notify( NOTI_ID, notification );

 

 

 

* 노티피케이션 객체의 추가설정

 

기본 사운드 설정

notification.defaults |= Notification.DEFAULT_SOUND;

 

특정 사운드 설정

notification.sound = Uri.parse("file:///sdcard/notification/my.mp3");

 

진동 추가

notification.defaults |= Notification.DEFAULT_VIBRATE;

 

notification.number

현재 이벤트의 갯수

 

notification.iconLevel

상태바에서 아이콘 에니메이션..LevelListDrawable 의 레벨

 

플래그

notification.flags 에 추가한다.

 

FLAG_AUTO_CANCEL : 알림 리스트 윈도에서 선택 후 자동으로 취소

FLAG_INSISTENT : 응답시까지 오디오 반복

FLAG_ONGOING_EVENT

FLAG_NO_CLEAR : Clear 버튼에 의해 없어져서는 안되는 알림

 

 

* 커스텀 알림 리스트 뷰

 

알림 항목들을 표시하는 리스트 윈도우에 표시되는 항목은 기본적으로

제목, 내용을 가지고 있는데, 해당 뷰도 변경 가능하다.

 

RemoteViews 객체를 사용해 해당 뷰에 대한 레이아웃을 정의할 수 있고,

notification.contentView 에 정의한 RemoteViews 객체를 지정하면 된다.

 

1. 커스텀 레이아웃 생성

custom_noti_layout.xml

이미지와 , 텍스트뷰를 하나씩 추가

 

2. contentView 지정

Remoteviews contentView = new Remoteviews( getPackageName(), R.layout.custom_noti_layout );

contentView.setImageviewResource( R.id.image, R.drawable.notification_image );

contentView.setTextViewText( R.id.text, "Hello" );

notification.contentView = contentView;

 

3. 인텐트 변경

커스텀 뷰를 사용하는 경우는 notification.setLatestEventInfo()

를 사용할 필요가 없다( 타이틀, 내용을 표시하지 않으므로..)

 

사용하는것이 아닌 필요한 내용으로 intent 구성 후,

notification.contentIntent 에 intent 정보를 설정하면 된다.

Intent notificationIntent = new Intent( this, MyClass.class );

PendingIntent contentIntent = PendingIntent.getActivity( this, 0, notificationIntent, 0 );

notification.contentIntent = contentIntent;

 

4. 알림 이벤트 전송은 동일

notiManager.notify( NOTI_ID, notification );