Notification

Functions to show different types of notifications

simple notification to display a title and description with a button to display a toast

Untitled

Just type in onCreate()

            val notification = simpleNotification( 
                context = requireContext(),
                title = "simple Notification Title",
                body = "Body of simple Notification",
                smallIcon = R.drawable.ic_baseline_notifications,
                Channel_Id = CHANNEL_ONE_ID,
                actionTitle = "Show Toast",
                actionIntent = actionIntent,
                contentIntent = clickPendingIntent
                ).build()
            notificationManager.notify(1, notification)

custom notification to show two layout one for collapsedView and other to expandedView

collapsedView

            val collapsedView = RemoteViews(
                requireContext().packageName,
                R.layout.notification_collapsed
                )
            collapsedView.setTextViewText(R.id.text_view_collapsed_1, "Hello World!")

image

expandedView

            val expandedView = RemoteViews(
                requireContext().packageName,
                R.layout.notification_expanded 
                )
            expandedView.setImageViewResource(R.id.image_view_expanded, R.drawable.download)
            expandedView.setOnClickPendingIntent(R.id.image_view_expanded, clickPendingIntent)

image

            val notification = customNotification(
                context = requireContext(),
                title = "Custom Notification Title",
                smallIcon = R.drawable.ic_baseline_notifications,
                Channel_Id = CHANNEL_ONE_ID,
                expandedView = expandedView,
                collapsedView = collapsedView
            ).build()
            notificationManager.notify(22, notification)

group chat with the ability to reply to the chat

image

remoteInput

            val remoteInput: RemoteInput = RemoteInput.Builder("key_text_reply")
                .setLabel("Your answer...")
                .build()

replyAction

            val replyAction: NotificationCompat.Action = NotificationCompat.Action.Builder(
                R.drawable.ic_baseline_notifications,
                "Reply",
                replyPendingIntent
            ).addRemoteInput(remoteInput).build()

chat style

            val messagingStyle = NotificationCompat.MessagingStyle("Me")
            messagingStyle.conversationTitle = "Group Chat"
            for (chatMessage in MESSAGES) {
                val notificationMessage = NotificationCompat.MessagingStyle.Message(
                    chatMessage.text,
                    chatMessage.timestamp,
                    chatMessage.sender
                )
                messagingStyle.addMessage(notificationMessage)
            }

            val notification = simpleNotification(
                context = requireContext(),
                title = "simple Notification Title",
                body = "Body of simple Notification",
                smallIcon = R.drawable.ic_baseline_notifications,
                Channel_Id = CHANNEL_ONE_ID,
                contentIntent = clickPendingIntent,
                style = messagingStyle
            ).addAction(replyAction)
                .build()
            notificationManager.notify(5, notification)

Notification to open a fragment from the Notification

pendingIntent FOR open fragment from notification

            val pendingIntent = NavDeepLinkBuilder(requireContext())
                .setComponentName(MainActivity::class.java) // your destination activity
                .setGraph(R.navigation.nav_graph)
                .setDestination(R.id.oneFragment)
                .createPendingIntent()

GitHub

View Github