Android Notification Builder — Complete Guide
In this tutorial, you'll learn about Android Notification Builder. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
The Problem
Your notification looks tiny with just a title and text. You can't expand it, or the big picture style doesn't show the image.
Wrong Approach ❌
// Basic notification — no expandable content
val notification = NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("New Message")
.setContentText("Hello!") // Short text only
.setSmallIcon(R.drawable.ic_message)
.build()
Output: Notification with no expandable content. Long text is truncated.
Right Approach ✅
// Expandable notification with BigTextStyle
val bigTextNotification = NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("New Message from Alice")
.setContentText("Hey, are you coming to the party tonight?")
.setSmallIcon(R.drawable.ic_message)
.setStyle(NotificationCompat.BigTextStyle()
.bigText("Hey, are you coming to the party tonight? It starts at 8pm and everyone will be there!")
.setBigContentTitle("Alice Smith")
.setSummaryText("1 new message")
)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true)
.setColor(ContextCompat.getColor(this, R.color.primary))
.build()
// BigPictureStyle for image notifications
val bigPictureNotification = NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("New Photo")
.setContentText("Alice shared a photo")
.setSmallIcon(R.drawable.ic_photo)
.setStyle(NotificationCompat.BigPictureStyle()
.bigPicture(bitmap) // The image to show
.bigLargeIcon(null) // Hide the large icon when expanded
)
.build()
// InboxStyle for multiple messages
val inboxNotification = NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("3 new messages")
.setStyle(NotificationCompat.InboxStyle()
.addLine("Alice: Hey!")
.addLine("Bob: Are you free?")
.addLine("Charlie: Meeting at 3")
.setSummaryText("3 new messages")
)
.build()
Output: Expandable notifications with proper styling.
Prevention
- Use
BigTextStylefor long text content. - Use
BigPictureStylefor image-sharing apps. - Use
InboxStylefor messaging apps with multiple messages. - Always set
setColor()to match your app's brand color.
Common Mistakes with notification Builder
- Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists
These mistakes appear frequently in real-world Android code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro