Android notifications and PendingIntent pitfalls
androidnotificationspendingintentchannelsmessagingstyle
Messaging-app notifications fail in ways that only show up in the field: a tap opens the wrong conversation, old read messages reappear, a hidden thread leaks its sender. Most causes trace back to a few Android behaviors that are easy to forget.
Wrong-conversation taps: filterEquals ignores extras
Two PendingIntents are "the same" if their Intents are filterEquals-equal (action, data, type, class, categories) and the request code matches. Extras are ignored. Tap intents that differed only by a thread_id extra collapsed into one record, and FLAG_UPDATE_CURRENT let the newest notification rewrite the target of older ones. Fix (the AOSP technique): put a unique data URI such as message://thread/<id> on every tap and action intent.
Side effect: a broadcast matched by a manifest <intent-filter> with no <data> element stops matching once it carries a data URI. Set an explicit component (setClass) as well.
Keep one authority for ids and policy
- One class mints every notification id, tag, request code and tap URI. Never mint inline. Foreground-service ids cannot be tagged; keep them positive (some OEMs mishandle negative ids).
- One policy function answers "should this notify?", in a fixed order (kid mode, quarantine, block/mute, spam, thread already on screen). Failure notifications go through it too: a "message failed to Alice" title leaks a hidden or private thread just as well as a message would.
- A redaction check that catches a database error and continues fails open (shows full content). Redaction must fail closed.
Stale history in MessagingStyle
Rebuilding the style from "the last 6 messages" re-posts already-read messages, including your own replies, on the next arrival. Build history from unread incoming messages plus your own messages newer than the newest unread. Add a setDeleteIntent that records a dismissal watermark (the newest rendered message timestamp) so a swiped-away notification does not resurrect. Dismissing a group summary may fire the children's delete intents; verify on the OEM.
Channels
- A channel's sound is immutable after creation. To change a per-conversation ringtone, create a new channel id (for example
messages_conv_<id>_s<hash-of-uri>) and delete the old one. - Per-conversation channels work from API 26; only
setConversationIdneeds API 30. Gating the whole scheme on API 30 silently killed custom ringtones on 26 to 29. - Hiding a thread (block, private, delete) must cancel its posted notifications and delete its channel; channel deletion alone only rescues API 30+.
Navigation from a tap
CLEAR_TOPwithoutSINGLE_TOPdestroys the running activity; use both and handleonNewIntent.- Consume the deep link once: act only when
savedInstanceState == nullandremoveExtraafter use, or rotation replays it. - Ignore intents with
FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY(relaunch from Recents). - Navigation with
launchSingleTopalone can open the wrong thread if two different results are tapped in one transition; guard on the current destination andpopUpTo.
Inline reply
- On failure, re-post or cancel the original notification, or the RemoteInput spinner spins forever (API 28+).
- Send the reply on the conversation's own SIM subscription.
- Mark-read from an action must not also call a second "mark all read"; the double write raced with inbound inserts and left phantom badges.
Gotchas
- Same-millisecond MMS WAP pushes shared a file name and PendingIntent; use UUID filenames.
- An audit with adversarial verifiers (agents told to refute each finding) was effective here: it also found that a fix for one bug was committed but not in the deployed APK (
git merge-base --is-ancestor <fix> <release-tag>proves it). After any hotfix branch deploy, merge back immediately.
See also
- Syncing an SMS app with the Android telephony provider
- Review agents before deploying nontrivial changes
Linked from
- Alarms and timers that ring on time (Android clock apps)
- App lock, PINs and brute-force lockout on Android
- FCM push notifications from a PHP backend to a Flutter app
- Message, Justin's Android SMS app (architecture and decisions)
- Recording audio and video on Android into MediaStore
- Sending MMS from an Android SMS app
- Syncing an SMS app with the Android telephony provider
- Unknown-sender quarantine and OTP exemptions for SMS
- Writing a replacement phone (dialer) app for Android