Skip to content

Latest commit

ย 

History

History
93 lines (78 loc) ยท 3.89 KB

File metadata and controls

93 lines (78 loc) ยท 3.89 KB

Expandable Notification(ํ™•์žฅํ˜• ์•Œ๋ฆผ)

Large image notification(ํฐ ์ด๋ฏธ์ง€ ์ถ”๊ฐ€ ์•Œ๋ฆผ)

Large block of text notification (ํฐ ํ…์ŠคํŠธ ๋ธ”๋ก ์•Œ๋ฆผ)

Inbox-style notification(๋ฐ›์€ํŽธ์ง€ํ•จ ์Šคํƒ€์ผ ์•Œ๋ฆผ)

Conversation in a notification(๋Œ€ํ™” ํ‘œ์‹œ ์•Œ๋ฆผ)

Media controls notification(๋ฏธ๋””์–ด ์ปจํŠธ๋กค ์•Œ๋ฆผ)

    private fun createNotification(
        context: Context,
        track: Track,
    ): Notification {
        val pendingIntent = PendingIntent.getActivity(
            context,
            0,
            Intent(context, PlayerActivity::class.java).apply {
                putExtra(PlayerActivity.KEY_PLAYER_TRACK, track)
                putExtra(PlayerActivity.KEY_BOTTOM_PLAYER_CLICK, true)
            },
            PendingIntent.FLAG_UPDATE_CURRENT
        )

        val pausePendingInt =
            PendingIntent.getService(
                context, 0,
                Intent(context, PlayerService::class.java)
                0,
            )

        val stopPendingIntent =
            PendingIntent.getService(
                context, 0,
                Intent(context, PlayerService::class.java).apply {
                    this.action = PlayerAction.STOP.value
                },
                0,
            )
        val stopAction = NotificationCompat.Action(
            R.drawable.ic_close_white,
            "",
            stopPendingIntent,
        )

        val appName = context.getString(R.string.app_name)
        val mediaSession = MediaSessionCompat(context, appName).apply {
            setMetadata(
                MediaMetadataCompat.Builder()
                    .putString(MediaMetadata.METADATA_KEY_TITLE, track.title)
                    .putString(MediaMetadata.METADATA_KEY_ARTIST, track.desc)
                    .build()
            )
        }

        return NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .addAction(NotificationCompat.Action(
                R.drawable.ic_pause_white,
                "",
                pausePendingInt,
            ))
            .addAction(stopAction)
            .setStyle(
                androidx.media.app.NotificationCompat.MediaStyle()
                    .setShowActionsInCompactView(0, 1)
                    .setMediaSession(mediaSession.sessionToken)
            )
            .setOngoing(true)
            .setContentTitle(context.getString(R.string.app_name))
            .setTicker(appName)
            .setContentIntent(pendingIntent)
            .build()
    }
  • setOngoing(boolean ongoing) : ์•Œ๋ฆผ ๋ฆฌ์ŠคํŠธ์—์„œ ์‚ฌ์šฉ์ž๊ฐ€ ๊ทธ๊ฒƒ์„ ํด๋ฆญํ•˜๊ฑฐ๋‚˜ ์ขŒ์šฐ๋กœ ๋“œ๋ž˜๊ทธํ•ด๋„ ์‚ฌ๋ผ์ง€์ง€ ์•Š์Œ ์„ค์ •
  • setTicker(CharSequence text) : ์•Œ๋ฆผ์ด ์ƒํƒœ ๋ฐ”์— ๋œฐ ๋•Œ ๊ทธ ๊ณณ์— ๋‚˜ํƒ€๋‚˜๋Š” ํ…์ŠคํŠธ
  • setStyle(NotificationCompat.Style style) : ์•Œ๋ฆผ ์Šคํƒ€์ผ ์ ์šฉ(MediaStyle, InboxStyle, BigTextStyle, BigPictureStyle, MessagingStyle, Etc..)
  • setVisibility(int visibility) SystemUI๊ฐ€ ์‹ ๋ขฐํ•  ์ˆ˜์—†๋Š” ์ƒํ™ฉ (์ฆ‰, ๋ณด์•ˆ ์ž ๊ธˆ ํ™”๋ฉด์—์„œ)์—์„œ ์•Œ๋ฆผ์˜ ์กด์žฌ์™€ ๋‚ด์šฉ์„ ํ‘œ์‹œํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ์‹œ๊ธฐ์— ์˜ํ–ฅ์„์ฃผ๋Š”์ด ์•Œ๋ฆผ์˜ ๊ฐ€์‹œ์„ฑ ๋ฒ”์œ„ ์„ค์ •