안드로이드를 개발하면서 많이 보게되는 deprecated.. 이전버전에서 지원하였지만 문제나 최신 API로 인하여 더 이상은 필요가 없는 경우에 이클립스에서 Notification 이런식으로 취소선을 쫙 그어준다.

이번에 처음으로 제대로 된 어플을 제작하는데 출시 마무리단계에서 좀 더 완벽해지고 싶은 마음에 눈에 거슬리는 노란색 느낌표들 모두를 제거하기 위해 deprecated를 제거해보았다.

이번 포스팅의 목표는 안드로이드 알림창인 Notification Class의 메소드인  Notification(icon, message, when)을 변경해보는 것이다.

기존 소스 코드
...(Context context, String message){
int icon = R.drawable.logo; 
long when = System.currentTimeMillis();
NotificationManager notificationManager =(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
Intent nIntent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, nIntent, 0);
notification.setLatestEventInfo(context, title, message, pIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(1234, notification);      
}


수정 소스 코드
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(getApplicationContext())
.setContentTitle("제목")
.setContentText("내용")
.setSmallIcon(R.drawable.ic_angle)
.setTicker("상태바제목")
.setAutoCancel(true)
.build();
notificationManager.notify(1234, notification);

두 코드의 메소드가 있고 없고 차이는 상관없음. ( 알림 하나 만들어두려면 수정 코드 처럼 정의해두고 Push처럼 Message를 전송하려면 메소드 정의해서 호출 후 notify하는 식으로 ...)

기존 코드에서는 Notification을 생성하며 정의 된 icon, message, when을 넣은 반면,
수정 코드에서는 Notification Builder를 이용하여 필요한 설정을 넣어주는 식이다.딱 봐도 훨씬 간결하고 수정하기 쉬워 보인다.


참조 
Notification - http://developer.android.com/reference/android/app/Notification.html
Intent - http://developer.android.com/reference/android/content/Intent.html
Context http://developer.android.com/reference/android/content/Context.html