MobileAndroidCreating Basic Notifications for an Android App

Creating Basic Notifications for an Android App

By Ashesh Shah

Notifications have a vital role to play when it comes to notifying app updates to users. This is especially useful with the app closed, wherein users know exactly when to open the app again for checking out notifications. However, it is also important that the notifications are useful and well timed.

A notification plays a critical role in bringing users back into the application, even if the app is closed. It is highly effective for users who have not checked the app for some time. Today, we are going to talk about how to create an Android notification. However, before that let us understand what it takes to build a notification.

Stepping Stones for a Notification

Whether you consider the simplest notifications possible or most complex ones, there are certain bare minimum requirements, to consider with notification coding:

  • Icon: Typical mobile users are always on the run, and there are hardly any possibilities of them checking out every single notification. So, how would a user differentiate between a relevant and an irrelevant notification? An icon acts as the differentiating factor, letting users know what the notification relates to. As an example, if a message has arrived, a postcard icon will clearly tell users that a message has arrived, and need to be seen instantly.
  • Title Text: Title text, accompanied with a notification, defines the notification in a textual format, as indicated by an icon.
  • Detailed Text: This is probably the most critical component of a notification, wherein there is a description of what the notification is about. The best way to get around with this is by explaining everything through brief and snappy text.

There are a lot other things associated with designing a notification. However, all those are optional, with only these three fulfilling the mandatory requirement of a fully functioning notification.

It Is Time to Create One

With Android evolving over time, you might wish to take advantage of the latest features related with notification, which are compatible with older versions of Android at the same time. For this, you need to use NotificationCompat, and the subclasses present within it. Because NotificationCompat is present in the Android Support Library, the first thing you need to do is open the build.gradle file inside the module level of project. Further, you need to add the support library to the dependencies section:

dependencies {

   ...

   compile "com.android.support:support-v4:24.1.1"
}

After the support library is added, now it is time to code the notification.

package com.jessicathornsby.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.NotificationManager;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.content.Context;
public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }
   public void sendNotification(View view) {
      // Get an instance of NotificationManager//
      NotificationCompat.Builder mBuilder =
         new NotificationCompat.Builder(this)
      .setSmallIcon(R.drawable.notification_icon)
      .setContentTitle("My notification")
      .setContentText("Hello World!");
      // Gets an instance of the NotificationManager service//
      NotificationManager mNotificationManager =
         (NotificationManager)
         getSystemService(Context.NOTIFICATION_SERVICE);
      // When issuing multiple notifications about the same type of event,
      // it's best for an app update existing notification with this new
      // information, rather than immediately creating a new notification.
      // If you want to update this notification later, you need to assign
      // it an ID. You can then use this ID whenever you issue a subsequent
      // notification. If the previous notification is still visible, the
      // system will update this existing notification, rather than create
      // a new one. Say for example, herein, the notifications ID is 001//
      NotificationManager.notify().
      mNotificationManager.notify(001, mBuilder.build());
   }
}

Do you want to test how this notification works about? Let us first open the activity_main.xml file and then have the following code in place.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns_android="http://schemas.android.com/
      apk/res/android"
   xmlns_tools="http://schemas.android.com/tools"
   android_layout_width="match_parent"
   android_layout_height="match_parent"
   tools_context="com.jessicathornsby.myapplication.MainActivity">
   <Button
      android_layout_width="wrap_content"
      android_layout_height="wrap_content"
      android_text="Do it!"
      android_id="@+id/button"
      android_onClick="sendNotification"/>
</LinearLayout>

To test the notification now, install the project on an Android Virtual Device (AVD), or a physical Android smartphone. Triggering a notification will only require you to tap anywhere on the screen, and what you see is ‘Hello World’ popping up.

Defining the Action

At this point, the notification is blank. Even if you tap on ‘Hello World,’ you will not see the notification actually doing anything. Creating a notification is followed by associating an action with the notification. A notification is meant to perform a certain activity when tapped. In simple terms, it is time to define an action. As an example, if you tap on an email notification, your Gmail app opens up.

Although thought to be an optional step in earlier days, today it is an unwritten rule to relate an action with a notification. If your users tap on a notification, and if nothing really happens, they will be left disappointed and not feel like clicking future notifications. Hence, relate at least one action with every single notification.

PendingIntent is used to define a notification action. In this example, let us use PendingIntent to let the notification open a default browser app, ultimately launching a Web site.

package com.jessicathornsby.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.NotificationManager;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.content.Context;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }
   public void sendNotification(View view) {
      NotificationCompat.Builder mBuilder =
         new NotificationCompat.Builder(this);
      // Create the intent that'll fire when the user
      // taps the notification//
      Intent intent = new Intent(Intent.ACTION_VIEW,
         Uri.parse("http://www.androidauthority.com/"));
      PendingIntent pendingIntent =
         PendingIntent.getActivity(this, 0, intent, 0);

      mBuilder.setContentIntent(pendingIntent);
      mBuilder.setSmallIcon(R.drawable.notification_icon);
      mBuilder.setContentTitle("My notification");
      mBuilder.setContentText("Hello World!");
      NotificationManager mNotificationManager =
         (NotificationManager)
         getSystemService(Context.NOTIFICATION_SERVICE);
      mNotificationManager.notify(001, mBuilder.build());
   }
}

Make appropriate changes, and then reinstall the project on your smartphone, tablet, or emulator. Ensure that you trigger the notification this time from the notification drawer, by giving it a tap. This will enable the phone’s default browser app to launch the intended Web site.

Setting the Priorities within a Notification

As a user, you might have seen multiple notifications popping up at the same time from the same app. How can your app prioritize which notifications to send earlier, and which ones later on? If too many notifications pop up together, users would not even bother to see even one of them, irrespective of whether or not it’s important.

Save your app from constantly flashing, ringing, or buzzing the users, allowing them lose interest and excitement in checking out a notification. Prioritizing the notifications and then sending them at the right times is very important, especially when you have too many of them lined up in a queue. So, how do you ensure that important notifications do not get lost amidst the crowd of messages?

Setting notification priorities is the key. You must have observed some notifications popping up, interrupting the users. They are set to high priority, which even light up the LED in many Android devices. This makes it difficult for the user to simply ignore them and proceed. Not assigning any priority allows notification to be assigned with a PRIORITY_DEFAULT state. Alternatively, let Android know the importance of your notification through the setPriority() method.

.setPriority(Notification.PRIORITY_MAX)

Summing Up Things

That is it. We have covered the basics of building notifications for your app. Of course, they are ground level basics; however, they do play an important role in setting a strong base for advanced level notifications. Depending on the kind of functionality you want with your app, you can go digging deeper, starting from basic notifications. You can send us your suggestions.

About the Author

Ashesh Shah is the CEO of Fusion Informatics Ltd. and co-founder of Digital Infoware Pvt. Ltd., which are progressive enterprises in the field of software, mobile app, and Web site design, as well as development services.

This article was contributed for exclusive use on Developer.com. All rights reserved.

Get the Free Newsletter!
Subscribe to Developer Insider for top news, trends & analysis
This email address is invalid.
Get the Free Newsletter!
Subscribe to Developer Insider for top news, trends & analysis
This email address is invalid.

Latest Posts

Related Stories