건프의 소소한 개발이야기

[안드로이드] Android Notification 구현하기 본문

개발 이야기/안드로이드 이야기

[안드로이드] Android Notification 구현하기

건강한프로그래머 2016. 4. 17. 13:17

안녕하세요, 건프입니다.


이번엔 안드로이드에서 Notification 을 관리하는 모듈 중 하나를 나름대로 구성해보았습니다.


일단 안드로이드의 노티피케이션을 등록하기 위한 과정을 볼께요


private void inside_sendNotification(String title, String message, int type){
Intent intent = new Intent(mContext, SplashActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext)
.setSmallIcon(R.drawable.egg)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);

NotificationManager notificationManager =
(NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(type /* ID of notification */, notificationBuilder.build());
}


- RingtoneManager 를 통해서 Default Notifiication Sound 를 받을 수 있습니다.

- NotificationCompat.Builder 를 이용해서 Icon, title, text 값 등 을 입력합니다.


이를 클래스 내부에 깔고, 저는 어플리케이션 내부에서 유동적으로 이용할 수 있도록 약간의 장치를 했습니다.


/**
* sendNotification 은 사용목적 및 편의를 고려해 두가지 형태를 지원합니다
* 기존에 등록된 사용목적에 부합하는 경우 (type 만으로 자동완성을 원할경우) type 만 인자로 넘기면
* default 값으로 실행합니다
* 만약 제목과 내용을 직접 담아야 할 경우에는 title, message 를 각각 첫번째 인자, 두번째 인자로 넘기면 됩니다
* @param type
*/
public void sendNotification(int type){
switch (type){
case BY_REVIEW_REGI:
inside_sendNotification(default_title, "새로운 리뷰가 성공적으로 등록되었습니다", type);
break;
default:
MapzipApplication.doLogging(TAG, "unknown notification type");
}

}
public void sendNotification(String title, String message, int type) {
inside_sendNotification(title, message, type);
}


위와 같이 두개의 함수로 오버로딩한 이유는, 타입에 따라서 이미 정해져있는 title과 text를 넘길경우, 매번 똑같은 내용을 반복해서 써야하거나, 같은내용을 다르게 쓰는것을 막기 위해서입니다.


만약 이비 정해져있는 타입(already defined type)의 경우라도, 타이틀과 텍스트를 사용자정의로 작성하고 싶을 경우를 대비해서 두번째 함수를 오버로딩 하였습니다.


이는 제 편의에 의한 방법이니, 각자의 편의에 따라 조정하시면 됩니다 :)


마지막으로 전체 소스 입니다~


/**
* Mapzip 서비스에서 Notification 기능을 이용해야할 때 사용합니다
* Created by myZZUNG on 2016. 4. 16..
*/
public class MapzipNotification {
private final String TAG = "MapzipNotification";

public static final int BY_GCM = 1;
public static final int BY_REVIEW_REGI = 2;

private Context mContext;

private final String default_title = "MapZip";

public MapzipNotification(Context context){
this.mContext = context;
}

/**
* sendNotification 은 사용목적 및 편의를 고려해 두가지 형태를 지원합니다
* 기존에 등록된 사용목적에 부합하는 경우 (type 만으로 자동완성을 원할경우) type 만 인자로 넘기면
* default 값으로 실행합니다
* 만약 제목과 내용을 직접 담아야 할 경우에는 title, message 를 각각 첫번째 인자, 두번째 인자로 넘기면 됩니다
* @param type
*/
public void sendNotification(int type){
switch (type){
case BY_REVIEW_REGI:
inside_sendNotification(default_title, "새로운 리뷰가 성공적으로 등록되었습니다", type);
break;
default:
MapzipApplication.doLogging(TAG, "unknown notification type");
}

}
public void sendNotification(String title, String message, int type) {
inside_sendNotification(title, message, type);
}

private void inside_sendNotification(String title, String message, int type){
Intent intent = new Intent(mContext, SplashActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext)
.setSmallIcon(R.drawable.egg)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);

NotificationManager notificationManager =
(NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(type /* ID of notification */, notificationBuilder.build());
}
}



사용법은 특별할 것이 없으니 생략할께요.


작은 도움이 되었다면 행복할 것 같습니다.

오늘 하루도 수고!

Comments