Note
Android Notificatoin 샘플 프로젝트는 아래 링크를 통해, 다운로드 받을 수 있습니다.
구현하려는 알림 채널에대한 상수를 선언합니다.
private static final String CHANNEL_ID = "channel_id";
private static final String PRIORITY_CHANNEL_ID = "pr_channel_id";
알림 채널을 만들어 NotificationManager 개체에 할당합니다.
private void createNotificationChannels() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel basicNotificationChanel = new NotificationChannel(CHANNEL_ID,
"basic notification chanel",
NotificationManager.IMPORTANCE_DEFAULT);
NotificationChannel priorityNotificationChannel = new NotificationChannel(PRIORITY_CHANNEL_ID,
"priority notification channel",
NotificationManager.IMPORTANCE_HIGH);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(basicNotificationChanel);
notificationManager.createNotificationChannel(priorityNotificationChannel);
}
}
createNotificationChannels()
를 호출한 후 NotificationManagerCompat 객체를 가져옵니다
notificationManagerCompat = NotificationManagerCompat.from(this);
알림표시
notificationManagerCompat.notify(id, builder.build());
//or
notificationManagerCompat.notify(id, notification);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notifications)
.setContentTitle("Basic Notification")
.setContentText("Text");
PendingIntent 생성
Intent intent = new Intent(this, Cat.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
.setContentIntent(pendingIntent)
를 사용하여 NotificationCompat.Builder 객체에 인텐트를 추가합니다. .setAutoCancel(true)
을 추가하면 이벤트 처리 후 알림을 닫습니다.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notifications)
.setContentTitle("Intent notification")
.setContentText("Click me")
.setContentIntent(pendingIntent)
.setAutoCancel(true);
기본 알림을 생성
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Progress bar notification")
.setContentText("Loading")
.setSmallIcon(R.drawable.ic_notifications);
스레드로 진행 상황을 증가시키고, 업데이트 처리 . .setProgress(int MAX_PROGRESS, int CURRENT_PROGRESS, boolean indeterminate)
final int PROGRESS_MAX = 100;
final int[] currentProgress = {0};
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if (currentProgress[0] == PROGRESS_MAX) {
builder.setContentText("Done")
.setProgress(0, 0, false);
notificationManagerCompat.notify(4, builder.build());
timer.cancel();
} else {
currentProgress[0] += 10;
builder.setProgress(PROGRESS_MAX, currentProgress[0], false);
notificationManagerCompat.notify(4, builder.build());
}
}
}, 0, 1000);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notifications)
.setContentTitle("Expandable text notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(getString(R.string.big_text)));
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notifications)
.setContentTitle("Expandable image notification")
.setContentText("Expand to see the full image")
.setLargeIcon(bitmap)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(bitmap)
.bigLargeIcon(null));
Notification notification = new Notification.Builder(this, CHANNEL_ID)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.ic_notifications)
.setContentTitle("Expandable media notification")
.setContentText("Artist")
.setLargeIcon(bitmap)
.addAction(R.drawable.ic_prev, "Previous", null) // #0
.addAction(R.drawable.ic_pause, "Pause", pendingIntent) // #1
.addAction(R.drawable.ic_next, "Next", null) // #2
.setStyle(new Notification.MediaStyle()
.setShowActionsInCompactView(1 /* #1: pause button */)
.setMediaSession(null))
.build();
여러줄 표시할 수 있는 알림
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notifications)
.setContentTitle("Inbox notification")
.setLargeIcon(bitmap)
.setStyle(new NotificationCompat.InboxStyle()
.addLine("First Line")
.addLine("Second Line")
.addLine("Third Line"));
중요도가 높은 채널은 .setPtiority(NotificationCompat.PRIORITY_HIGH)
와 PRIORITY_CHANEL_ID를 사용합니다.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, PRIORITY_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notifications)
.setContentTitle("Foreground Notification")
.setContentText("This is important!!!")
.setPriority(NotificationCompat.PRIORITY_HIGH);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, PRIORITY_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notifications)
.setContentTitle("Incoming call")
.setContentText("(919) 555-1234")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setFullScreenIntent(fullScreenPendingIntent, true);
안드로이드에서는 네트워크나 파일을 통해 오는 데이터에서 개행 문자 적용을 하기 위해서는 아래와 같이 추가적인 처리가 필요합니다. 전달받는 message를 아래 소스코드를 참고하여 한번 더 처리하여 notification에 적용하시기 바랍니다.
String strBody = message
String strBody2 = "";
strBody2 = strBody.replace("\\r\\n", "\r\n");
strBody2 = strBody2.replace("\\n", "\n");
strBody2 = strBody2.replace("\\r", "\r");