개발일지

Android in A..Z - Notification (Extend) 본문

Android (안드로이드)/Notification

Android in A..Z - Notification (Extend)

강태종 2021. 7. 15. 16:24

확장형 알림

기존의 알림보다 훨씬 많은 알림을 제공하기 위해 확장형 알림을 사용할 수 있다.


코드

@Singleton
class ExtendNotificationManager @Inject constructor(
    @ApplicationContext
    private val context: Context
) {
    companion object {
        private const val CHANNEL_ID_IMPORTANCE_HIGH = "com.taetae98.notification.EXTEND.IMPORTANCE_HIGH"
        private const val CHANNEL_ID_IMPORTANCE_LOW = "com.taetae98.notification.EXTEND.IMPORTANCE_LOW"
        private const val CHANNEL_NAME = "Extend"
        private const val CHANNEL_DESCRIPTION = "Extend Notification"
    }

    private val manager by lazy { context.getSystemService(NotificationManager::class.java) }

    fun imageNotify(message: Message, bitmap: Bitmap) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createNotificationChannel(CHANNEL_ID_IMPORTANCE_HIGH, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH)
        }

        val notification = NotificationCompat.Builder(context, CHANNEL_ID_IMPORTANCE_HIGH)
            .setSmallIcon(R.drawable.ic_android)
            .setContentTitle(message.title)
            .setContentText(message.message)
            .setCategory(NotificationCompat.CATEGORY_MESSAGE)
            .setStyle(
                NotificationCompat.BigPictureStyle()
                    .bigPicture(bitmap)
            )
            .build()

        manager.notify(message.id, notification)
    }

    fun progressNotify(message: Message) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createNotificationChannel(CHANNEL_ID_IMPORTANCE_HIGH, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH)
            createNotificationChannel(CHANNEL_ID_IMPORTANCE_LOW, CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW)
        }

        CoroutineScope(Dispatchers.IO).launch {
            repeat(101) {
                val notification = NotificationCompat.Builder(context, if(it == 100) CHANNEL_ID_IMPORTANCE_HIGH else CHANNEL_ID_IMPORTANCE_LOW)
                    .setPriority(if (it == 100) NotificationCompat.PRIORITY_MAX else NotificationCompat.PRIORITY_MIN)
                    .setSmallIcon(R.drawable.ic_android)
                    .setContentTitle(message.title)
                    .setContentText(message.message)
                    .setProgress(100, it, it <= 10)
                    .setCategory(NotificationCompat.CATEGORY_PROGRESS)
                    .build()

                manager.notify(message.id, notification)
                delay(100L)
            }
        }
    }

    @RequiresApi(Build.VERSION_CODES.O)
    private fun createNotificationChannel(id: String, name: String, importance: Int) {
        val channel = NotificationChannel(id, name, importance).apply {
            description = CHANNEL_DESCRIPTION
        }

        manager.createNotificationChannel(channel)
    }
}

setStyle을 통해서 기본으로 제공하는 Template을 사용할 수 있다.


Git

https://github.com/KangTaeJong98/Example/tree/main/Android/Notification

 

KangTaeJong98/Example

My Example Code. Contribute to KangTaeJong98/Example development by creating an account on GitHub.

github.com

 

Comments