Android provides three primary notification types for engaging users: standard, expandable, and heads-up notifications.
Standard Notifications
Standard notifications appear in the system notification drawer. Use the NotificationCompat.Builder class to construct them.
private void displayStandardNotification() {
// Define the action when the notification is tapped
Intent notificationIntent = new Intent(this, DetailActivity.class);
notificationIntent.putExtra("NOTIFICATION_ID", 101);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
// Build the notification
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "channel_id")
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("App Alert")
.setContentText("New message received.")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setAutoCancel(true); // Dismiss on tap
// Issue the notification
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(101, notificationBuilder.build());
}
To programmatically dismiss a specific notification, call cancel() with its unique ID.
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(101);
Expandable Notifications
Expandbale notifications reveal more content when the user pulls down on them. Use setStyle() with a BigTextStyle or BigPictureStyle.
private void displayExpandableNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_id")
.setSmallIcon(R.drawable.alert_icon)
.setContentTitle("Weather Update")
.setContentText("Today's forecast...")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("Detailed forecast: Sunny skies with a high of 75°F. Winds will be light from the northwest."));
NotificationManagerCompat.from(this).notify(102, builder.build());
}
Heads-Up Notifications
Heads-up notifications appear as a floating banner at the top of the screen for high-priority alerts. Set a high priority and optionally a vibration pattern.
private void displayHeadsUpNotification() {
Intent fullScreenIntent = new Intent(this, UrgentActivity.class);
PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 0, fullScreenIntent, PendingIntent.FLAG_IMMUTABLE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "urgent_channel")
.setSmallIcon(R.drawable.warning_icon)
.setContentTitle("Incoming Call")
.setContentText("John Doe is calling...")
.setPriority(NotificationCompat.PRIORITY_HIGH) // Required for heads-up
.setCategory(NotificationCompat.CATEGORY_CALL)
.setFullScreenIntent(fullScreenPendingIntent, true); // Triggers heads-up display
NotificationManagerCompat.from(this).notify(103, builder.build());
}
Notification Visibility on Lock Screen
Android allows control over notification visibility on secured lock screens using the setVisibility() method.
// Show notification on all lock screens
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
// Hide sensitive content on secure lock screens
builder.setVisibility(NotificationCompat.VISIBILITY_PRIVATE);
// Hide notification entirely on secure lock screens
builder.setVisibility(NotificationCompat.VISIBILITY_SECRET);