Skip to content

Android Notification PendingIntent — Complete Guide

DodaTech Updated 2026-06-24 2 min read

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_IMMUTABLE on API 33+ (required by Android).
  • Use FLAG_UPDATE_CURRENT to update extras when the same PendingIntent is reused.
  • Use unique requestCode for different notification types.
  • Set FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP for launcher intents.

Common Mistakes with notification pending intent

  1. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  2. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  3. 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

### What is the difference between FLAG_IMMUTABLE and FLAG_MUTABLE?

FLAG_IMMUTABLE prevents the intent from being modified by the receiver (secure). FLAG_MUTABLE allows modifications. Use FLAG_IMMUTABLE unless you need inline replies or custom actions.

### Why does my PendingIntent have stale extras?

You're not using FLAG_UPDATE_CURRENT. Without it, Android caches the first PendingIntent and reuses it. Add FLAG_UPDATE_CURRENT to update extras.

### What is requestCode in PendingIntent.getActivity()?

It disambiguates PendingIntents. Use a unique requestCode for each notification type so different intents don't collide in the pending intent cache.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro