알림 만들기 실습
build.gradle에
뷰바인딩 추가
viewBinding {
enabled = true
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="@+id/notificationButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="알림 발생" />
</LinearLayout>
activity_main.xml
java파일폴더 - 마우스우클릭 - new - other - Broadcast Receiver
package com.example.ch10_notification
import android.app.NotificationManager
import android.app.RemoteInput
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
class ReplyReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
// 알림 입력 글 획득
val replyTxt = RemoteInput.getResultsFromIntent(intent)
?.getCharSequence("key_text_reply")
Log.d("hahee", "replyTxt : $replyTxt")
// 알림 취소
val manager = context.getSystemService(
AppCompatActivity.NOTIFICATION_SERVICE
) as NotificationManager
manager.cancel(11)
}
}
ReplyReceiver
package com.example.ch10_notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Intent
import android.graphics.BitmapFactory
import android.media.AudioAttributes
import android.media.RingtoneManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat
import androidx.core.app.RemoteInput
import com.example.ch10_notification.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.notificationButton.setOnClickListener {
val manager = getSystemService(NOTIFICATION_SERVICE)
as NotificationManager
val builder: NotificationCompat.Builder
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//26버전 이상
val channelId = "one-channel"
val channelName = "My Channel One"
val channel = NotificationChannel(
channelId,
channelName,
NotificationManager.IMPORTANCE_DEFAULT
).apply {
//채널에 다양한 정보 설정
description = "My Channel One EventLogTags.Description"
setShowBadge(true)
val uri: Uri = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val audioAttributes = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build()
setSound(uri, audioAttributes)
enableVibration(true)
}
manager.createNotificationChannel(channel)
//채널을 이용하여 builder 생성
builder = NotificationCompat.Builder(this,channelId)
} else {
builder = NotificationCompat.Builder(this)
}
builder.run {
setSmallIcon(R.drawable.small)
setWhen(System.currentTimeMillis())
setContentTitle("하히")
setContentText("안녕하세요.")
setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.big))
}
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(
R.drawable.send,
"답장",
replyPendingIntent
).addRemoteInput(remoteInput).build()
)
manager.notify(11,builder.build())
}
}
}
MainActivity
- 결과 -
'Android Studio' 카테고리의 다른 글
안드로이드 #9 : Resource 230306 (0) | 2023.03.06 |
---|---|
안드로이드 #8 : chronometer 스탑워치 앱 230306 (0) | 2023.03.06 |
안드로이드 #7 : 이벤트 처리하기 230303 (0) | 2023.03.03 |
안드로이드 #6 : 전화걸기 화면에서 숫자누르기, back버튼, #(리셋) 기능 추가(이벤트 실습) 230303 (0) | 2023.03.03 |
안드로이드 #5 : 전화걸기 화면 만들어보기(레이아웃 실습) 230303 (0) | 2023.03.03 |
댓글