728x90
Notification은 애플리케이션과 별도로 관리되는 메시지 이다.
Notification 메시지를 OS에게 요청하면 OS는 알림 창 영역에 알림 메시지를 표시한다.
화면을 가지지 않는 실행단위에서 메시지를 표시할 때 주로 사용한다.
특징
사용자가 메시지를 확인하거나 제거하기 전까지 메시지를 유지한다.
메시지를 터치하면 지정된 Activity가 실행되어 애플리케이션 실행을 유도할 수 있다.
Channel
안드로이드 8.0 부터 새롭게 추가된 기능
이전에는 사용자가 설정에서 알림 메시지를 비활성화 하면 모든 메시지가 비활성화 되었다.
8.0 부터는 Notification Channel을 이용하애 알림 메시지를 채널이라는 그룹으로 묶어 관리할 수 있으며 사용사는 채널 별로 메시지 활성화 여부를 설정할 수 있다.
package com.example.notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.Color
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.app.NotificationCompat
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener {
val builder1 = getNotificationBuilder("channer1","첫 번째 채널")
// 작은 아이콘(메세지 수신시 상단에 보여줄 작은 아이콘)
builder1.setSmallIcon(android.R.drawable.ic_menu_search)
// 큰 아이콘(메시지 본문에 표시할 메시지, bitmap 객체)
val bitmap = BitmapFactory.decodeResource(resources,R.mipmap.ic_launcher)
builder1.setLargeIcon(bitmap)
// 숫자 설정
builder1.setNumber(100)
// 타이틀 설정
builder1.setContentTitle("Content Title 1")
//메세지 설정
builder1.setContentText("Content Text 1")
// 메세지 객체를 생성
val notification = builder1.build()
// 알림 메세지를 관리하는 객체를 추출한다.
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
//알림 메시지를 출력한다.
manager.notify(10,notification)
}
button2.setOnClickListener {
val builder1 = getNotificationBuilder("channer1","첫 번째 채널")
// 작은 아이콘(메세지 수신시 상단에 보여줄 작은 아이콘)
builder1.setSmallIcon(android.R.drawable.ic_menu_search)
// 큰 아이콘(메시지 본문에 표시할 메시지, bitmap 객체)
val bitmap = BitmapFactory.decodeResource(resources,R.mipmap.ic_launcher)
builder1.setLargeIcon(bitmap)
// 숫자 설정
builder1.setNumber(100)
// 타이틀 설정
builder1.setContentTitle("Content Title 2")
//메세지 설정
builder1.setContentText("Content Text 2")
// 메세지 객체를 생성
val notification = builder1.build()
// 알림 메세지를 관리하는 객체를 추출한다.
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
//알림 메시지를 출력한다.
manager.notify(20,notification)
}
button3.setOnClickListener {
val builder1 = getNotificationBuilder("channer2","두 번째 채널")
// 작은 아이콘(메세지 수신시 상단에 보여줄 작은 아이콘)
builder1.setSmallIcon(android.R.drawable.ic_menu_search)
// 큰 아이콘(메시지 본문에 표시할 메시지, bitmap 객체)
val bitmap = BitmapFactory.decodeResource(resources,R.mipmap.ic_launcher)
builder1.setLargeIcon(bitmap)
// 숫자 설정
builder1.setNumber(100)
// 타이틀 설정
builder1.setContentTitle("Content Title 3")
//메세지 설정
builder1.setContentText("Content Text 3")
// 메세지 객체를 생성
val notification = builder1.build()
// 알림 메세지를 관리하는 객체를 추출한다.
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
//알림 메시지를 출력한다.
manager.notify(30,notification)
}
button4.setOnClickListener {
val builder1 = getNotificationBuilder("channer2","두 번째 채널")
// 작은 아이콘(메세지 수신시 상단에 보여줄 작은 아이콘)
builder1.setSmallIcon(android.R.drawable.ic_menu_search)
// 큰 아이콘(메시지 본문에 표시할 메시지, bitmap 객체)
val bitmap = BitmapFactory.decodeResource(resources,R.mipmap.ic_launcher)
builder1.setLargeIcon(bitmap)
// 숫자 설정
builder1.setNumber(100)
// 타이틀 설정
builder1.setContentTitle("Content Title 4")
//메세지 설정
builder1.setContentText("Content Text 4")
// 메세지 객체를 생성
val notification = builder1.build()
// 알림 메세지를 관리하는 객체를 추출한다.
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
//알림 메시지를 출력한다.
manager.notify(40,notification)
}
}
// 안드로이드 8.0 이상과 미만버전에 대응하기 위해 채널을 설정하는 메서드 (id 내부적 아이디, name, 화면에 보여줄 이름)
fun getNotificationBuilder(id: String, name: String): NotificationCompat.Builder {
// os 버전별로 분기
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //안드로이드 8.0 이상이라면
//알림 메세지를 관리하는 객체를 추출한다.
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// 채널 객체를 생성한다.
val channel = NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH)
// 메시지 출력시 단말기 LED를 사용할 것인가
channel.enableLights(true)
// LED 생상 설정
channel.lightColor = Color.RED
// 알림 메시지를 관리하는 객체에 채널을 등록한다.
manager.createNotificationChannel(channel)
val builder = NotificationCompat.Builder(this, id)
return builder
} else {
val builder = NotificationCompat.Builder(this)
return builder
}
}
}
728x90
'안드로이드 > 메세징' 카테고리의 다른 글
안드로이드 다양한 Notification (0) | 2022.06.13 |
---|---|
안드로이드 PendingIntent (0) | 2022.06.13 |
안드로이드 항목 선택 Dialog (0) | 2022.06.13 |
안드로이드 ListDialog (0) | 2022.06.13 |
안드로이드 Dialog (0) | 2022.06.13 |