Understanding Notifications in Android: A Developer’s Guide
Enhance User Engagement with Secure and Effective Notifications

Introduction
Notifications are an essential part of Android apps, informing users of events, alerts, or updates. Effective use of notifications enhances user experience, engagement, and retention. This article explains how Android notifications work, including their components, lifecycle, security considerations, and best practices.
How Android Notifications Work
Android notifications are managed by the NotificationManager
. Each notification is displayed in the notification drawer, offering users quick access to app functionality and information without opening the app directly.
Components of a Notification
A notification typically consists of:
- Small Icon: Required; represents your app in the notification drawer.
- Title and Content Text: Briefly describe the notification’s purpose.
- Intent: Determines what happens when the user taps the notification.
- Actions: Optional buttons that allow users to interact directly from the notification.
- Channel: Required for Android Oreo (API level 26) and above; groups notifications by category.
Notification Channels
Starting with Android Oreo (8.0, API 26), notification channels are mandatory. Channels categorize notifications by type, allowing users to selectively control notification settings such as sound, vibration, and visibility.
Example of creating a notification channel:
val channel = NotificationChannel(
CHANNEL_ID,
"Messages",
NotificationManager.IMPORTANCE_HIGH
).apply {
description = "Channel for chat messages"
}
notificationManager.createNotificationChannel(channel)
Creating a Simple Notification
Here’s how to build and display a basic notification:
val builder = NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("New Message")
.setContentText("You have received a new message.")
.setPriority(NotificationCompat.PRIORITY_HIGH)
with(NotificationManagerCompat.from(this)) {
notify(notificationId, builder.build())
}
Notification Actions
Notification actions enhance user interactivity by allowing direct responses or quick actions without fully opening the app. Actions appear as buttons below the notification content.
Common use cases include:
- Replying to a message directly from the notification
- Marking a task as completed
- Snoozing reminders or alarms
To implement notification actions that navigate users to a specific destination within an app having multiple navigation paths, follow these detailed steps:
Step 1: Create the Action Intent
Define the intent to trigger navigation to a specific destination (e.g., fragment or activity):
val actionIntent = Intent(this, MainActivity::class.java).apply {
putExtra("EXTRA_SCREEN_ID", R.id.specificFragment)
flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
}
Step 2: Create a PendingIntent
Wrap the intent into a PendingIntent
:
val actionPendingIntent = PendingIntent.getActivity(
this, 0, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
Step 3: Handle Navigation in Your App
In your MainActivity
, handle the intent to navigate to the specified screen:
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
intent?.extras?.getInt("EXTRA_SCREEN_ID")?.let { screenId ->
findNavController(R.id.nav_host_fragment).navigate(screenId)
}
}
Notification Lifecycle
Notifications remain visible until:
- User dismisses manually.
- App dismisses using
notificationManager.cancel(notificationId)
. - User taps notification, which triggers the associated intent.
Security Considerations
When dealing with sensitive data, notifications should be carefully handled:
- Visibility Control: Set notification visibility to private or secret to prevent sensitive content from showing on the lock screen.
- Encryption: Ensure sensitive data transmitted or stored for notifications is encrypted.
- Avoid Sensitive Content in Plain Text: Avoid displaying personal or sensitive details directly. Provide generic notification text prompting users to open the app for secure viewing.
Notification Visibility Levels
The visibility levels available in Android notifications are:
- VISIBILITY_PUBLIC: Shows notification content on the lock screen.
- VISIBILITY_PRIVATE: Hides sensitive notification content until the device is unlocked, displaying only the notification icon and title.
- VISIBILITY_SECRET: Completely hides notifications from the lock screen.
Using NotificationCompat.VISIBILITY_PRIVATE
ensures sensitive details are visible only after the user unlocks their device. This is ideal for notifications containing sensitive or personal information.
Example to control notification visibility:
val builder = NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("Secure Message")
.setContentText("Unlock to view message")
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
Handling Users Without Secure Lock Screens
If the user has no secure lock screen (no PIN, password, pattern, or biometric security), sensitive notifications set to VISIBILITY_PRIVATE
will still be fully visible. In such scenarios:
- Check Device Security: Use
KeyguardManager
to verify whether the device has a secure lock configured. - Fallback Strategy: If no security is configured, either show generic, non-sensitive messages or prompt users to enable device security for sensitive content.
Example of checking device security:
val keyguardManager = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
if (keyguardManager.isDeviceSecure) {
// Show sensitive notification
} else {
// Show non-sensitive notification or prompt user to secure device
}
Best Practices for Notifications
- Respect User Preferences: Use channels effectively, allowing users control over notification frequency and type.
- Be Timely and Relevant: Deliver notifications that are timely and actionable.
- Avoid Overwhelming Users: Limit frequency and provide meaningful information.
Testing Notifications
Use emulator tools or devices to test how notifications appear, ensuring they work correctly across various Android versions and screen sizes.
Conclusion
Understanding and properly implementing notifications significantly enhances user experience. Following best practices and security considerations ensures notifications effectively serve their purpose, benefiting both the user and your app’s user engagement.
Dobri Kostadinov
Android Consultant | Trainer
Email me | Follow me on LinkedIn | Follow me on Medium | Buy me a coffee