Select Page

Notifications provide short, timely, and relevant information about your app. Notifications are intended to inform users about events in your app. Users can choose to view the notification while using other apps and respond to it when it’s convenient for them.

Required contents :

As per the official documentation, A Notification object must contain the following

  1. Title : title, set by setContentTitle().
  2. Detail : detail, set by setContentText().
  3. Small Icon : Icon, set by setSmallIcon().

Standard notification :

To create notifications you have to use NotificationCompat.Builder’s object. The notification itself build by NotificationCompat.Builder.build(), which returns an object containing the specifications of the notifications.

NotificationHelper is a utility class consist of the methods used to create the NotificationBuilder and trigger the notification.The following snippet illustrates a simple notification that specifies an activity to open when the user clicks.

public void showStandardNotification() {
int notificationId = new Random().nextInt();
Intent intent = new Intent(this, NotificationActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder notification =NotificationHelper.createNotificationBuider(this,
"Notification", "Standard notification!", R.drawable.ic_notifications, pIntent);
NotificationHelper.showNotification(this, notificationId, notification.build());
}

Standard Notification

Heads-up notification :

Notifications can appear in a small floating window when the device is active. It also shows action buttons. Users can act on, or dismiss, a heads-up notification without leaving the current app. The following snippet shows heads-up notifications and action buttons.

public void showHeadsUpNotification() {
int notificationId = new Random().nextInt();
Intent intent = new Intent(this, NotificationActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
Intent positive = new Intent(this, NotificationReceiver.class);
positive.putExtra("notiID", notificationId);
positive.setAction(POSITIVE_CLICK);
PendingIntent pIntent_positive = PendingIntent.getBroadcast(this, notificationId, positive, PendingIntent.FLAG_CANCEL_CURRENT);
Intent negative = new Intent(this, NotificationReceiver.class);
negative.putExtra("notiID", notificationId);
negative.setAction(NEGATIVE_CLICK);
PendingIntent pIntent_negative = PendingIntent.getBroadcast(this, notificationId, negative, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder notification = NotificationHelper.createNotificationBuider(this,
"Notification", "Heads-up notification", R.drawable.ic_notifications, pIntent);
notification.setPriority(Notification.PRIORITY_HIGH).setVibrate(new long[0]);
notification.addAction(new NotificationCompat.Action(R.drawable.ic_notifications, "Postive", pIntent_positive));
notification.addAction(new NotificationCompat.Action(R.drawable.ic_notifications, "Negative", pIntent_negative));
NotificationHelper.showNotification(this, notificationId, notification.build());
}

Headsup Notification

Replying to notification :

Android N that allow the user to respond directly from the notification without the need to open our app to do so. On a handheld, the inline reply action appears as an additional button displayed in the notification. When a user replies via keyboard, the system attaches the text response to the intent. The below code helps you to create a notifications action that supports direct reply.

public void showRemoteInputNotification() {
int notificationId = new Random().nextInt();
NotificationCompat.Builder notification = NotificationHelper.createNotificationBuider(this,
"Remote Input", "Notification with Remote Input", R.drawable.ic_notifications);
notification.setGroupSummary(true);
notification.setGroup("KEY_NOTIFICATION_GROUP1");
Intent remote_intent = new Intent(this, NotificationReceiver.class);
remote_intent.putExtra("notiID", notificationId);
remote_intent.setAction(REPLY_CLICK);
PendingIntent pIntent_positive = PendingIntent.getBroadcast(this, notificationId, remote_intent, PendingIntent.FLAG_CANCEL_CURRENT);
RemoteInput remoteInput = new RemoteInput.Builder(REPLY_TEXT_KEY)
.setLabel("Type Something")
.build();
// Create the reply action and add the remote input.
NotificationCompat.Action action =
new NotificationCompat.Action.Builder(R.drawable.ic_send, "Reply", pIntent_positive)
.addRemoteInput(remoteInput)
.build();
notification.addAction(action);
// Color change
notification.setColor(ContextCompat.getColor(this, R.color.colorAccent));
NotificationHelper.showNotification(this, notificationId, notification.build());
}

To receive user input from the remote input, you can use the following snippet

private CharSequence getMessageText(Intent intent) {
Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
if (remoteInput != null) {
return remoteInput.getCharSequence(NotificationHelper.REPLY_TEXT_KEY);
}
return null;
}

Reply to notification

Bundling notifications :

Android N provides developers with a new way to represent a queue of notifications: bundled notifications. Now you can bundle the related notification in your application using this feature. Use the below code to implement this into your application.

public void showSummaryNotification() {
int notificationId = new Random().nextInt();
Intent intent = new Intent(this, NotificationActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(),
R.drawable.icon_email);
NotificationCompat.Builder summary_notification = NotificationHelper.createNotificationBuider(this,
"2 new messages", "Bundle notification!", R.drawable.ic_call, pIntent);
summary_notification.setStyle(new NotificationCompat.InboxStyle()
.addLine("Ashwin Check this out")
.addLine("Ranjith Launch Party")
.setBigContentTitle("2 new messages")
.setSummaryText("2 new messages"))
.setLargeIcon(largeIcon)
.setGroupSummary(true)
.setContentIntent(pIntent)
.setGroup(GROUP_KEY).build();
int notificationId1 = new Random().nextInt();
NotificationCompat.Builder notification1 = NotificationHelper.createNotificationBuider(this,
"Ashwin", "Check this out", R.drawable.ic_notifications, pIntent);
int notificationId2 = new Random().nextInt();
NotificationCompat.Builder notification2 = NotificationHelper.createNotificationBuider(this,
"Ranjith", "Launch Party", R.drawable.ic_notifications, pIntent);
NotificationHelper.showNotification(this, notificationId1, notification1.build());
NotificationHelper.showNotification(this, notificationId2, notification2.build());
NotificationHelper.showNotification(this, notificationId, summary_notification.build());
}

SummaryNotification

I made a simple sample app for Android Notifications, it’s over on GitHub if you want to check it out.

View on GithubDownload .zip

Feel free to comment and share, keep watching this space get more updates on Android Stuff!

Elsewhere

Jaison Fernando

Android Developer at NFN Labs
Elsewhere