1. Notification Manager 얻기
NotificationManager mNotification = Context.getSystemService(NOTIFICATION_SERVICE);

2. Notification 객체 만들기
Notification notifyDetails = new Notification(int icno, CharSequence tickerText, long When);
ex) Notification notifyDetails = new Notification(res, text, System.currentTimeMillis());

3. Notification 객체 셋팅하기..
먼저 Notification 필드를 확인해 봅시다.
- number :  통지아이콘에 겹쳐서 출력될 숫자를 지정한다. 예를 들어 새로운 메시지가 도착했다는 통지라면 메시지의 개수를 같이 표시할 수 있다. 0이나 음수를 지정하면 숫자가 표시되지 않는다.
- sound : 통지와 함께 출력할 소리를 Uri객체로 지정한다.
- vibrate : 진동 방식을 지정한다. 진동할 시간과 멈출 시간을 배열로 전달함으로써 진동의 패턴을 지정한다.
- ledARGB : 불빛의 색상을 지정한다. 장치에 장착된 LED의 능력에 따라 표현 가능한 색상은 조금 달라질 수 도 있다.
- ledOnMs, ledOffMS : LED를 켤 시간과 끌 시간을 1/1000초(밀리세컨드) 단위로 지정한다. 이 두 값을 LED의 점멸 주기를 결정한다. 정확하지는 않지만 가급적 근접한 시간을 지킨다.
- default : 디폴트로 취할 통지 전달 방식을 지정
   DEFAULT_SOUND : 소리 발생
   DEFAULT_VIBRATE : 진동을 발생
   DEFAULT_LIGHTS : 불빛을 깜빡 거린다.
   DEFAULT_ALL : 세가지 동작 다.
- flag : 통지의 동작 방식을 지정
  FLAG_AUTO_CANCEL : 통지를 누르면 자동으로 통지를 취소
  FLAG_INSISTENT : 취소하거나 상태란을 확장하기 전까지 소리를 계속 발생
  FLAG_NO_CLEAR : 사용자가 clear all 을 선택할 때 취소한다.
  FLAG_ONGOING_EVENT : 계속 진행중인 이벤트를 참조
  FLAG_ONLY_ALERT_ONCE : 이전에 취소된 통지라도 매번 소리와 진동을 발생
  FLAG_SHOW_FIGHTS : LED 불빛을 출력한다.


4. 확장 상태란과 선택시에 동작
Void setLatestEventInfo (context context, CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent);
통지가 발생시킨 주체와 제목, 메시지 내용 문자열 등을 지정하고 통지뷰를 눌렀을 때 호출할 인텐트도 지정.


5. 마지막으로 통지를 등록하거나 취소하기
void notify(int id, Notification notification);
void cancel(int id);
void cancelAll();
* notify는 첫번째 인자 id는 통지 객체의 고유한 식별 번호이다. 한 프로그램 내에서 유일한 값이어야 한다.
  이 id를 통해서 통지 객체의 아이콘을 변경하거나 취소할 때 사용할 수 있다.

6. Tip
한프로그램내에서 여러개의 통지를 등록한 상태에서 Intent를 실행시 마지막에 등록된 통지만 Intent할 때가 있다.
이럴때는 PendingIntent의 requestCode를 다르게 주면된다.
PendingIntent content = PendingIntent.getActivity(SmartControl.this, noti_id, intent, Intent.FLAG_ACTIVITY_NEW_TASK );
그래도 잘 안될땐.. 플래그를 줘보자 ㅋㅋ Intent.FLAG_ACTIVITY_NEW_TASK

7. 예제 소스
  1. NotificationManager mNotification = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);  
  2. Notification notifyDetails =   
  3.     new Notification(res, text, System.currentTimeMillis());  
  4.   
  5. long[] vibrate = { 100100200300 };  
  6. notifyDetails.vibrate = vibrate;  
  7. notifyDetails.defaults = Notification.DEFAULT_ALL;  
  8. notifyDetails.flags |= Notification.FLAG_NO_CLEAR;  
  9.   
  10. Intent intent = new Intent(SmartControl.this,  
  11.         DialogMacroLoopEdit.class);  
  12.   
  13. PendingIntent content =   
  14.     PendingIntent.getActivity(SmartControl.this, noti_id, intent, Intent.FLAG_ACTIVITY_NEW_TASK );  
  15.   
  16. notifyDetails.setLatestEventInfo(SmartControl.this,"Title""Test", content);  
  17. mNotificationManager.notify(noti_id, notifyDetails);  

+ Recent posts