상단바에 시스템 정보, 알림, 앱정보 등을 출력하는 것
상태바는 원래 시스템에서 관리하므로 앱이 직접 제어를 할 수 없다. 따라서 앱에서 API를 통해 시스템에 요청을 해서 알림을 출력한다.
NotificationChannel -> NotificationCompat.Builder() -> Notification -> notify()
API 레벨 26 버전부터는 채널 개념을 도입하여 앱마다 채널별로 받고싶은 알람을 선택해서 받을 수 있다. ( 기존에는 앱 알림을 끄면 모든 알림이 꺼졌다. )
알림 중요도
같이 사용되는 알림 관련 함수들
알림 빌더 설정
val manager = getSystemService(AppCompatActivity.NOTIFICATION_SERVICE) as NotificationManager
val builder: NotificationCompat.Builder
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
val channelId = "Channel-Id"
val channelName = "Channel Name"
val channel = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH)
//채널 정보 설정
channel.description = "Description"
channel.setShowBadge(true)
val uri : Uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val audioAttributes = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build()
channel.setSound(uri, audioAttributes)
channel.enableLights(true)
channel.lightColor = Color.BLUE
channel.enableVibration(true)
channel.vibrationPattern = longArrayOf(100,200,200,100)
manager.createNotificationChannel(channel)
builder = NotificationCompat.Builder(this,channelId)
}else{
builder = NotificationCompat.Builder(this)
}
builder.setSmallIcon(android.R.drawable.ic_notification_overlay)
builder.setWhen(System.currentTimeMillis())
builder.setContentTitle("Content Title")
builder.setContentText("Content Message")
manager.notify(11, builder.build())
빌더에 알림 내용 설정 후 manager를 통해 notify()를 실행한다.( 아래는 실행 결과 )
notify() 의 첫번째 매개변수값은 알림을 식별하는 데 사용하며 개발자가 임의로 지정한다.
아래와 같이 알림을 지우지 못하도록 설정했을 경우, 특정 이벤트 때 cancel()을 사용하여 알림을 지워야 한다.
builder.setAutoCancel(false) //알림 터치 시 알림이 사라지지 않음
builder.setOngoing(true) //스와이프로 알림 지워지지 않음
manager.cancel(11) // 매개변수는 notify()에서 개발자가 지정한 값
val intent = Intent(this, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(this, 10, intent, PendingIntent.FLAG_IMMUTABLE)
builder.setContentIntent(pendingIntent)
KClass<*>
로 선언 된 API면 Sample::class 로 작성Class<*>
로 선언되었으면 자바로 작성된 API 이므로 Sample::class.java 로 작성val actionIntent = Intent(this, OneReceiver::class.java)
val actionPendingIntent = PendingIntent.getBroadcast(this, 20, actionIntent, PendingIntent.FLAG_IMMUTABLE)
builder.addAction(
NotificationCompat.Action.Builder(android.R.drawable.stat_notify_more, "Action", actionPendingIntent).build()
)
//원격 입력
val KEY_TEXT_REPLY = "key_text_reply"
var replyLabel : String = "답장 입력"
var remoteInput : RemoteInput = RemoteInput.Builder(KEY_TEXT_REPLY).run{
setLabel(replyLabel)
build()
}
//인텐트 준비
val replyIntent = Intent(this, ReplyReceiver::class.java)
val replyPendingIntent = PendingIntent.getBroadcast(this, 30, replyIntent, PendingIntent.FLAG_MUTABLE)
//원격 입력 액션 등록하기
builder.addAction(
NotificationCompat.Action.Builder(android.R.drawable.send, "답장", replyPendingIntent)
.addRemoteInput(remoteInput).build()
)
val replyTxt = RemoteInput.getResultsFromIntent(intent)?.getCharSequence("key_text_reply")
builder.setProgress(100, 0, false)
manager.notify(11, builder.build())
thread{
for(i in 1..100){
builder.setProgress(100, i, false)
manager.notify(11, builder.build())
SystemClock.sleep(100)
}
}
val bigPicture = BitmapFactory.decodeResource(resources, R.drawable.test)
val bigStyle = NotificationCompat.BigPictureStyle()
bigStyle.bigPicture(bigPicture)
builder.setStyle(bigStyle)
val bigTextStyle = NotificationCompat.BigTextStyle()
bigTextStyle.bigText(resources.getString(R.string.long_text))
builder.setStyle(bigTextStyle)
val style = NotificationCompat.InboxStyle()
style.addLine("Line 1")
style.addLine("Line 2")
builder.setStyle(style)
val sender1 : Person = Person.Builder()
.setName("lumineko")
.build()
val sender2 : Person = Person.Builder()
.setName("lumine")
.build()
val message1 = NotificationCompat.MessagingStyle.Message(
"hello", System.currentTimeMillis(), sender1
)
val message2 = NotificationCompat.MessagingStyle.Message(
"hi", System.currentTimeMillis(), sender2
)
val messageStyle = NotificationCompat.MessagingStyle(sender1)
.addMessage(message1)
.addMessage(message2)
builder.setStyle(messageStyle)