3월 222014
 

※ 이 문서는 Notification 자체에 대해서 설명하고 있는 것이 아니라 Notification을 알고 있다는 가정하에서 만들어야 할 앱이 API Level 11 미만을 지원해야할  경우를 위한 설명이다. Notification 자체에 대한 이해는 [1]을 참조하도록 한다.

안드로이드 Notification을 구현하는 방법은 여러가지가 있는데 하나는 기본 제공하는 양식을 사용하는 방법, 다른 하나는 Custom layout을 이용하여 구현하는 방법이 있다. 이 중에 기본 제공 양식을 사용하는 방법으로 구현을 해 보고자 한다.

하지만 안타깝게도 타겟 플렛폼 버젼이 8(Android 2.2, Froyo)이상으로 해야 했기 때문에 문제가 발생했다. 바로 기본 제공 방법을 사용할 경우에 Notification의 생성자가 deprecated 됬기 때문이다. 즉 사용하지 않는 것이 좋다는 뜻이다. 바로 이 메소드가 문제!

The notification constructor is deprecated

Notification의 생성자 Reference[2]

   빨간 네모박스의 내용과 같이 SDK 11버젼부터는 Notification.Builder를 대신 사용하라는 것을 알 수 있다.  그러면 어떻게 해야할까? 여러 인터넷 사이트를 찾아보던 도중 한 사이트[3]를 발견했고, 그곳에 따르면 빌드 환경에 따라서 다른 메소드를 사용할 수 있게 해준다는 것이였다. 아래와 같은 방법으로 가능하다.

 

 

 

if(Build.VERSION.SDK_INT >= 11) {
   // 안드로이드 플렛폼 버젼(API Level)이 11 이상일 경우 실행
} else {
   // 안드로이드 플렛폼 버젼이 11 미만일 경우 실행
}

즉  Notification을 11미만일 때와 11 이상일 경우를 다르게 하여 구현하면 된다는 것! 아래 코드를 보자

int icon = context.getApplicationInfo().icon // Notification 발생시 출력할 아이콘
String title = ""; // Notification 발생시 아래로 내렸을 때 첫번째 줄
String subtitle = ""; // Notification 발생시 아래로 내렸을 때 두번째 줄
String ticker = ""; // 이벤트 발생시 위에 출력될 내용 
Notification noti;

if(Build.VERSION.SDK_INT >= 11) {
    noti = new Notification.Builder(context)
      .setContentTitle(title)
      .setContentText(subtitle)
      .setTicker(ticker)
      .setSmallIcon(icon)
      .setContentIntent(pIntent)
      .build();
} else {
    noti = new Notification(icon, ticker, System.currentTimeMillis());
    noti.setLatestEventInfo(context, title, subtitle, pIntent);
}

위와 같이 구현하면 최소 SDK 버젼에 관계 없이 구현가능하다. 실제로는 위와 같이 구현하면 최소 API Level이 8이기 때문에 11에서 추가된 Notification.Builder는 이용할 수 없지만 위와 같이 하면 문제없이 빌드 가능하다. 만약 에러가 난다면 프로젝트를 clean하여 다시 빌드하도록  하자.

API 레벨에 대해서는 [3][4]을 참조하도록 한다.

 

 참고자료


[1] Android Developer – Notifications,  https://developer.android.com/guide/topics/ui/notifiers/notifications.html
[2] Android Developer – References, https://developer.android.com/reference/android/app/Notification.html#Notification(int, java.lang.CharSequence, long)
[3] 【android】OSのバージョン(APIレベル)によって処理を変える方法, http://nobuo-create.net/api/
[4] Android Developer – API Level, http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels