Android Notification PendingIntent — Complete Guide
In this tutorial, you'll learn about Android Notification PendingIntent. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
The Problem
Tapping a notification opens the right screen the first time but a stale screen the second time. Or the PendingIntent triggers twice.
Wrong Approach ❌
// No flags — may reuse stale intent
val intent = Intent(this, MainActivity::class.java).apply {
putExtra("screen", "profile")
}
val pendingIntent = PendingIntent.getActivity(this, 0, intent, 0) // Flags = 0
// Mutable PendingIntent on API 33+ — security risk
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_MUTABLE)
Output: Notification opens same screen regardless of extras. Security warning for mutable intent.
Right Approach ✅
// Tap to open Activity
val intent = Intent(this, MainActivity::class.java).apply {
putExtra("screen", "profile")
putExtra("userId", "123")
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
}
val pendingIntent = PendingIntent.getActivity(
this,
requestCode = 1001, // Unique per notification
intent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT // API 33+
)
// Tap to open deep link
val deepLinkIntent = Intent(Intent.ACTION_VIEW, "myapp://profile/123".toUri(), this, MainActivity::class.java)
val deepLinkPendingIntent = PendingIntent.getActivity(this, 1002, deepLinkIntent, PendingIntent.FLAG_IMMUTABLE)
// Tap to dismiss with BroadcastReceiver
val dismissIntent = Intent(this, DismissReceiver::class.java).apply {
action = "ACTION_DISMISS"
putExtra("notificationId", 42)
}
val dismissPendingIntent = PendingIntent.getBroadcast(this, 1003, dismissIntent, PendingIntent.FLAG_IMMUTABLE)
// Build notification
val notification = NotificationCompat.Builder(this, CHANNEL_ID)
.setContentIntent(pendingIntent) // Tap
.setDeleteIntent(dismissPendingIntent) // Swipe to dismiss
.setAutoCancel(true)
.build()
Output: Correct screen opens on tap. PendingIntent is immutable (secure).
Prevention
- Always use
FLAG_IMMUTABLEon API 33+ (required by Android). - Use
FLAG_UPDATE_CURRENTto update extras when the same PendingIntent is reused. - Use unique
requestCodefor different notification types. - Set
FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOPfor launcher intents.
Common Mistakes with notification pending intent
- Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
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