[Android Studio] Notification

Darcy Daeseok YU ·2022년 6월 12일
0

리액트를 개발하다가 안드로이드 스튜디오로 기능을 구현하기
feat.Kotlin

말해뭐해. 시작

알림 생성 흐름

알림 채널 생성 -> 알림 생성 & 알림 표시 -> 알림 클릭시 pendingIntent 스크린 표시

먼저 채널을 생성한다. (특정버전 이상부터 필수임)

  1. 알림 채널 생성 메소드 (페이지 컨텍스트, 채널이름, 채널 설명) 을 파라미터로 받는다.
fun createNotificationChannel(
            context: Context,
            name: String,
            channelDescription: String,
        ) {
            //알림 채널 아이디
            channel_id = SystemClock.currentThreadTimeMillis().toString()

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                // 중요도 설정
                val importance = NotificationManager.IMPORTANCE_HIGH
                //채널 생성
                val channel = NotificationChannel(channel_id, name, importance).apply {
                    description = channelDescription
                }

                notificationManager =
                    context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
                notificationManager?.createNotificationChannel(channel)
            }
        }
  1. 채널을 생성하고 알림을 생성하고 실행과 동시에 알림을 보낸다.
fun displayNotification(context: Context, file_path: String) {
            req_num += 1
            noti_id += 1

            val intent = Intent(context, VideoAlarmAndNotifiActivity::class.java).apply {
                putExtra(Constants.VIDEO_PATH, file_path)
            }
            val pendingIntent = PendingIntent.getActivity(
                context,
                req_num,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT
            )

            val notificationId = noti_id
            val notification = NotificationCompat.Builder(context, channel_id).apply {
                setSmallIcon(R.drawable.ic_launcher_foreground)
                setContentTitle("Example")
                setContentText("Example contents")
                setDefaults((Notification.DEFAULT_LIGHTS or Notification.DEFAULT_VIBRATE))
                setCategory(NotificationCompat.CATEGORY_ALARM)
                setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                setAutoCancel(false)
                setShowWhen(true)
                setContentIntent(pendingIntent)
                setFullScreenIntent(pendingIntent, true)
                priority = NotificationCompat.PRIORITY_MAX
            }
            notificationManager?.notify(notificationId, notification.build())
        }

실사용 예제

class yourActivity : AppCompatActivity(){

//알림 매니저를 전역변수로 
private var notificationManager: NotificationManager? = null


	override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
        
       findViewById(R.id.button1).setOnClickListener{
			//채널 생성             				VideoAlarmAndNotifiActivity.createNotificationChannel(applicationContext, "TestChannel", "This is a test Channel")
            //알림 생성
             	VideoAlarmAndNotifiActivity.displayNotification(applicationContext,path)
       }
     
        
    }



}

버튼을 클릭하면 채널이 생성되고 알림이 표시된다
알림을 클릭하면 intent -> pendingIntent에 설정해준 화면으로 이동한다.

profile
React, React-Native https://darcyu83.netlify.app/

0개의 댓글