android heads up notification

냠냠·2022년 5월 12일
0

android

목록 보기
3/5

notification 을 표시할 때 아래와 같이 상단에 펼쳐진 채로 보여지는 걸 heads up notification 이라고 한다.

여러가지 설정해주어야 할 것이 많지만 중요한건 setFullScreenIntent 를 해주어야 한다는 것이다. 해당 메서드를 이용하면

  • 유저가 디바이스를 사용중 : heads up notification 으로 보여준다. 해당 notification 을 터치하면 intent 에 설정된 activity 가 뜬다 (터치시 acticity 가 뜨는건 됐다가 안됐다가 해서 쫌 더 확인해봐야 한다)
  • 디바이스 화면이 꺼져있을때 : 파라미터로 넘겨준 intent 에 설정된 activity 가 뜬다.

여기서는 heads up notification 으로 보여주기 위한 방법까지만 알아보고 디바이스가 꺼져 있을 때 acticity 화면으로 띄워주길 원하면 Android Alarm 만들기 여기를 참고하길 바란다.

  1. 권한설정
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT"/>
  1. channel 과 notification 우선순위를 IMPORTANCE_HIGH 로 설정
  2. setFullScreenIntent 설정
        val channelId = "Download"
        val notificationId = Math.random().toInt()

        val manager = NotificationManagerCompat.from(context)
        val channel = NotificationChannelCompat
            .Builder(channelId, NotificationManagerCompat.IMPORTANCE_HIGH)
            .setName("File Download")
            .build()

        manager.createNotificationChannel(channel)

        val intent = Intent(context, DialogActivity::class.java)
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_NO_ANIMATION
        val pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)
        val builder = NotificationCompat.Builder(context, channel.id)
            .setSmallIcon(icon)
            .setTicker("Downloading...")
            .setContentTitle("Downloading...")
            .setPriority(NotificationManagerCompat.IMPORTANCE_HIGH)
            .setAutoCancel(false)
            .setFullScreenIntent(pendingIntent, true)

        manager.notify(notificationId, builder.build())
  1. 그리고 만약 notification Priority가 변경되었다면 앱을 삭제 후 재설치 하고 테스트 하는것을 권장한다. (정확하지는 않으나 notification channel 관련 옵션은 코드상에서 변경되어도 앱을 재설치 해야 적용되는것으로 보인다)

0개의 댓글