Building reliable Android notifications with NotificationCompat
Android apps often need to alert people about a due task, a message, or an account event even when the main screen is closed. A notification must therefore work consistently across Android releases, respect user settings, and open the correct part of the app when tapped.
NotificationCompat gives application code one API for these requirements. It is useful for Australian developers whose users may have Google Pixel phones, Samsung Galaxy devices, or older handsets still using mobile data. The compatibility layer does not override channels, permissions, battery controls, or Doze behaviour; those system rules still apply.
Preparing the notification builder
Add the current stable androidx.core:core-ktx dependency to the app module. Both NotificationCompat and NotificationManagerCompat are supplied by AndroidX Core, so the rest of the code can use one consistent API.
On Android 8.0/API 26, every notification must belong to a channel. Create channels once during application startup, using names people can recognise, and avoid deleting and recreating them to force fresh settings. Repeated calls with the same channel ID are safe, but each category should have its own channel.
private const val CHANNEL_ID = "task_reminders"
private fun postReminder() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
CHANNEL_ID,
"Task reminders",
NotificationManager.IMPORTANCE_DEFAULT
)
getSystemService(NotificationManager::class.java)
.createNotificationChannel(channel)
}
val notification = NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("Task due")
.setContentText("Review your Android project notes")
.setStyle(
NotificationCompat.BigTextStyle()
.bigText("Review your Android project notes")
)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true)
.build()
// On Android 13+, post only after notification permission is granted.
NotificationManagerCompat.from(this).notify(101, notification)
}
Supply a monochrome small icon drawn as a white silhouette on a transparent background; Android masks and recolours it for the status bar. setPriority influences older releases, while channel importance governs Android 8.0 and later. setAutoCancel removes the alert after its content is opened.
Connecting taps and notification actions
A tap should carry enough context to open the exact task. Create an explicit Intent for the destination activity, add a task ID as an extra, and wrap it in a PendingIntent. A distinct request code prevents one pending intent from accidentally replacing another.
val openTask = PendingIntent.getActivity(
this,
42,
Intent(this, TaskActivity::class.java)
.putExtra("task_id", "42"),
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
Add it to the builder with .setContentIntent(openTask) before building the notification. TaskActivity can then read task_id and open the corresponding record.
Notification actions such as Complete or Snooze use a broadcast PendingIntent directed at a dedicated receiver. Build the action with NotificationCompat.Action.Builder, then attach it through .addAction(...). Include the task ID in the explicit intent so the receiver knows which item to change.
Keep the pending intent immutable unless a trusted component genuinely needs to add fields later. FLAG_IMMUTABLE prevents another process from modifying it, while FLAG_UPDATE_CURRENT refreshes its extras when the notification is updated. Declare an internal broadcast receiver with android:exported="false".
Managing channels and notification permission
Android 8.0 and later let users change channel importance, sound and vibration independently of the app. A high-importance channel should be reserved for genuinely urgent alerts because users can lower or silence it. Check app-wide status with NotificationManagerCompat.from(context).areNotificationsEnabled() and inspect the relevant channel before posting.
Android 13/API 33 also requires the POST_NOTIFICATIONS runtime permission. Add <uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> to the manifest, check it with ContextCompat.checkSelfPermission, and request it through ActivityResultContracts.RequestPermission when the user enables reminders. If permission is denied, the app should continue without posting and explain how to reopen system settings instead of repeatedly prompting.
Keep sensitive information off the lock screen with setVisibility(NotificationCompat.VISIBILITY_PRIVATE) and provide a generic public version where appropriate. Australian transport, banking and health apps should avoid exposing account details or authentication codes. For covered organisations, the Privacy Act 1988 and Australian Privacy Principles require transparent and secure handling of personal information. Commercial push messages may also fall under the Spam Act 2003, making consent and a clear opt-out important where required.
Updating, cancelling, and scheduling alerts
Notification identity controls replacement and removal. Posting with the same ID updates the existing notification, while a different ID creates another entry in the shade. Use a stable ID per task, call cancel(id) when the task is resolved, and set setOnlyAlertOnce(true) on updates that should not replay a sound.
Use WorkManager for deferred local reminders rather than expecting a Handler or background thread to survive process death. It is not an exact-alarm mechanism, so avoid second-level promises. For Australian schedules, store an IANA zone such as Australia/Sydney or Australia/Melbourne and calculate the next local occurrence. Melbourne and Sydney change their clocks, whereas Brisbane does not, so daylight-saving transitions need explicit handling.
Choose local notifications for actions based on information already on the device, and Firebase Cloud Messaging for server-triggered events. Remote delivery depends on connectivity: a commuter in a Sydney or Melbourne underground station may receive a message later than expected. Critical information should therefore remain available inside the app rather than existing only as a transient banner.
Testing and comparing delivery approaches
Test on emulators and physical devices covering API 26, API 29, API 33 and the current release. Pixel and Samsung Galaxy devices are widely used in Australia’s Android market, and their background policies can differ. Verify the shade, lock screen, tap navigation, actions, reboot recovery, Doze and channel settings; a visible shade entry does not guarantee a heads-up popup.
| Approach | Best suited to | Behaviour | Main limitation |
|---|---|---|---|
| NotificationCompat.Builder | Local alerts across Android versions | Builds notifications through the AndroidX compatibility layer | Channels, permissions and background policies still apply |
| Platform Notification.Builder | Apps targeting a known modern API | Exposes newer platform features directly | Requires version-specific implementation |
| WorkManager | Deferred local reminders | Runs eligible work later and can post the result | Timing is approximate and it does not render notifications |
| Firebase Cloud Messaging | Server-triggered alerts | Delivers through Google’s messaging service | Requires connectivity, project setup and privacy review |
Build checklist
- Create notification channels before the first alert on Android 8.0+.
- Give every notification a stable ID and a valid PendingIntent.
- Use a proper small icon and handle the Android 13 permission.
- Test taps and actions while the app is backgrounded.
Release checklist
- Confirm lock-screen content and public-version behaviour.
- Check notification settings after reboot and on Samsung devices.
- Review Privacy Act and Spam Act implications for data and promotions.
- Measure delivery and tap events without logging sensitive details.
Reliable alerts depend on a clear channel, permission-aware messaging, stable identity and a tested navigation path. NotificationCompat supplies compatibility; the app remains responsible for timing, privacy and user expectations.