개발일지

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

Android (안드로이드)/Notification

Android in A..Z - Notification (Custom)

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

Notification Custom

사용자가 직접 Layout을 구성하여 RemoteView를 통해 Nofitication 템플릿을 만들 수 있습니다.

* 보통 축소된 Notification은 64dp, 확장된 Notification은 256dp로 제한됩니다.


 

RemoteViews를 만들고setCustomContentView를 통해 적용할 수 있다.

* 확장된 CustomContentView는 setCustomBigContentView를 통해 적용할 수 있다.

    private fun createCustomContentView(): RemoteViews {
        return RemoteViews(context.packageName, R.layout.notification_content_view).apply {
            setOnClickPendingIntent(
                R.id.lock,
                PendingIntent.getBroadcast(
                    context,
                    1000,
                    Intent(context, CustomNotificationReceiver::class.java)
                        .apply {
                            action = CustomNotificationReceiver.ACTION_LOCK
                        }
                    , 0
                )
            )
            setOnClickPendingIntent(
                R.id.lock_open,
                PendingIntent.getBroadcast(
                    context,
                    1000,
                    Intent(context, CustomNotificationReceiver::class.java)
                        .apply {
                            action = CustomNotificationReceiver.ACTION_LOCK_OPEN
                        }
                    , 0
                )
            )
            setOnClickPendingIntent(
                R.id.rotation,
                PendingIntent.getBroadcast(
                    context,
                    1000,
                    Intent(context, CustomNotificationReceiver::class.java)
                        .apply {
                            action = CustomNotificationReceiver.ACTION_ROTATION
                        }
                    , 0
                )
            )
            setOnClickPendingIntent(
                R.id.portrait,
                PendingIntent.getBroadcast(
                    context,
                    1000,
                    Intent(context, CustomNotificationReceiver::class.java)
                        .apply {
                            action = CustomNotificationReceiver.ACTION_PORTRAIT
                        }
                    , 0
                )
            )
            setOnClickPendingIntent(
                R.id.landscape,
                PendingIntent.getBroadcast(
                    context,
                    1000,
                    Intent(context, CustomNotificationReceiver::class.java)
                        .apply {
                            action = CustomNotificationReceiver.ACTION_LANDSCAPE
                        }
                    , 0
                )
            )
        }
    }
    
    fun notify(message: Message) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createNotificationChannel()
        }

        val notification = NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_android)
            .setShowWhen(false)
            .setStyle(NotificationCompat.DecoratedCustomViewStyle())
            .setCustomContentView(createCustomContentView())
            .build()

        manager.notify(message.id, notification)
    }

코드

class CustomNotificationManager @Inject constructor(
    @ApplicationContext
    private val context: Context
) {
    companion object {
        private const val CHANNEL_ID = "com.taetae98.notification.CUSTOM"
        private const val CHANNEL_NAME = "Custom"
        private const val CHANNEL_DESCRIPTION = "This is Custom Notification"
    }

    private val manager by lazy {
        NotificationManagerCompat.from(context)
    }

    fun notify(message: Message) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createNotificationChannel()
        }

        val notification = NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_android)
            .setShowWhen(false)
            .setStyle(NotificationCompat.DecoratedCustomViewStyle())
            .setCustomContentView(createCustomContentView())
            .build()

        manager.notify(message.id, notification)
    }

    @RequiresApi(Build.VERSION_CODES.O)
    private fun createNotificationChannel() {
        val channel = NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH).apply {
            description = CHANNEL_DESCRIPTION
        }

        manager.createNotificationChannel(channel)
    }

    private fun createCustomContentView(): RemoteViews {
        return RemoteViews(context.packageName, R.layout.notification_content_view).apply {
            setOnClickPendingIntent(
                R.id.lock,
                PendingIntent.getBroadcast(
                    context,
                    1000,
                    Intent(context, CustomNotificationReceiver::class.java)
                        .apply {
                            action = CustomNotificationReceiver.ACTION_LOCK
                        }
                    , 0
                )
            )
            setOnClickPendingIntent(
                R.id.lock_open,
                PendingIntent.getBroadcast(
                    context,
                    1000,
                    Intent(context, CustomNotificationReceiver::class.java)
                        .apply {
                            action = CustomNotificationReceiver.ACTION_LOCK_OPEN
                        }
                    , 0
                )
            )
            setOnClickPendingIntent(
                R.id.rotation,
                PendingIntent.getBroadcast(
                    context,
                    1000,
                    Intent(context, CustomNotificationReceiver::class.java)
                        .apply {
                            action = CustomNotificationReceiver.ACTION_ROTATION
                        }
                    , 0
                )
            )
            setOnClickPendingIntent(
                R.id.portrait,
                PendingIntent.getBroadcast(
                    context,
                    1000,
                    Intent(context, CustomNotificationReceiver::class.java)
                        .apply {
                            action = CustomNotificationReceiver.ACTION_PORTRAIT
                        }
                    , 0
                )
            )
            setOnClickPendingIntent(
                R.id.landscape,
                PendingIntent.getBroadcast(
                    context,
                    1000,
                    Intent(context, CustomNotificationReceiver::class.java)
                        .apply {
                            action = CustomNotificationReceiver.ACTION_LANDSCAPE
                        }
                    , 0
                )
            )
        }
    }
}

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