class 클래스명 : AppCompatActivity() {
private lateinit var binding: 바인딩
lateinit var builder: NotificationCompat.Builder
lateinit var manager: NotificationManager
//오레오 이상은 반드시 채널을 설정해줘야 Notification이 작동함
private val CHANNEL_ID = "channel1"
private val CHANNEL_NAME = "Channel1"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = 바인딩.inflate(layoutInflater, null, false)
binding.testAlarm.setOnClickListener(){
pushAlert(this)
}
setContentView(binding.root)
}
}
private fun pushAlert(context : Context)
{
val am: AlarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
manager.createNotificationChannel(
NotificationChannel(
CHANNEL_ID,
CHANNEL_NAME,
NotificationManager.IMPORTANCE_DEFAULT
)
)
builder = NotificationCompat.Builder(context, CHANNEL_ID)
//알림창 클릭 시 activity 화면 부름
val intent2 = Intent(context, MainActivity::class.java)
val pendingIntent =
PendingIntent.getActivity(context, 101, intent2, PendingIntent.FLAG_UPDATE_CURRENT)
//알림창 제목
builder.setContentTitle("알람")
//알림창 아이콘
builder.setSmallIcon(R.drawable.ic_launcher_background)
//알림창 터치시 자동 삭제
builder.setAutoCancel(true)
builder.setContentIntent(pendingIntent)
val notification: Notification = builder.build()
manager.notify(1, notification)
}
'프로그래밍 > Android' 카테고리의 다른 글
[Android] Calendar를 이용해 특정시간에 푸시알림 보내기 (1) | 2023.11.24 |
---|---|
[Android] AlarmManager (1) | 2023.11.24 |
[Android] ProgressBar 구현 (1) | 2023.11.22 |
[Android] Room (4) | 2023.11.21 |
[Android] 숫자 천단위 콤마찍기 (0) | 2023.07.20 |
댓글