Notification

Reading Time: 4 minutes

A notification is a message that Android displays outside your app’s UI to provide the user with reminders, communication from other people, or other timely information from your app. Users can tap the notification to open your app or take an action directly from the notification.

When you issue a notification, it first appears as an icon in the status bar.
Users can swipe down on the status bar to open the notification drawer, where they can view more details and take actions with the notification.

Users can drag down on a notification in the drawer to reveal the expanded view, which shows additional content and action buttons,  of provided. A notification remains visible in the notification drawer until dismissed by the app or the user.

Step 1

First we will create a MainApplication class, what extends the Application class.

Starting in Android 8.0 (API level 26), all notifications must be assigned to a channel. For each channel, you can set the visual and auditory behavior that is applied to all notifications in that channel. Then, users can change these settings and decide which notification channels from your app should be intrusive or visible at all. To create a notification channel, follow these steps.

      1. Construct a NotificationChannel object with a unique Channel ID, a user- visible name, and an importance level.
      2. Optionally, specify the description that the user sees in the system settings with setDescription().
      3. Register the notification channel by passing it to createNotificationChannel().

We will implement in the MainApplication the NotificationChannel so at the startup of the app the channel will be created.

Before the source code we should clarify 2 things.
Channel ID: This will identify the notification. Later on we can update it with this.
Importance level: Channel importance affects the interruption level of all notifications posted in the channel, and you must specify it in the NotificationChannel constructor. You can use one of five importance levels, ranging from IMPORTANCE_NONE(0) to IMPORTANCE_HIGH(4).
The importance level you assign to a channel applies to all notification messages that you post to it.

MainApplication::class


MainApplication::class

Step 2

After the implementation of the MainApplication, you have to add it to the AndroidManifest.xml.


AndroidManifest.xml

Step 3

The next step is to create the activity_main.xml.

We will have here two EditText as an input type for the title and the text of the notification.

The third of will be a Button, what will responsible to create the and send out the notification.

We will align this views with ConstraintLayout.


activity_main.xml

Step 4

The final step is to implement the notificaiton in the MainActivity::class.

In the onCreate method will be the setOnClickListner for the Button, what will fire the sendNotificaiton method.

When we click on the Button, it will first check the content of the two EditText. If at least one of them is empty, a Toast will pop up with a warning: “Title or Text is empty!”

Notification compatibility
Since Android 1.0, the notification system UI and the notification-related APIs have continually evolved. To use the latest notification API features while still supporting older devices, use the support library notification API: NotificationCompat and its subclasses, as well as NotificationManagerCompat. This will allow you to avoid writing conditional code to check API levels because these APIs handle that for you.

NotificationCompat is updated as the platform evolves to include the latest methods. It is important to note that the availability of a method in NotificationCompat does not guarantee that the corresponding feature will be provided on older devices. In some cases calling a newly-introduced API results in a no-op on older devices. For example, NotificationCompat.addAction() displays the action button on a device running Android 4.1 (API level 16) and higher only.

Notification compatibility

addAction

  • Add an action to the notification. Actions are typically displayed by the system as a button adjacent to the notification content. Action buttons won’t appear on platforms prior to Android 4.1. Action buttons depend on expanded notifications, which are only available in Android 4.1 and later. To ensure that an action button’s functionality is always available, first implement the functionality in the Activity that starts when a user clicks the notification (see setContentIntent()), and then enhance the notification by implementing the same functionality with addAction().
  • Set the small icon to use in the notification layouts.
  • Set the relative priority for the notification. Priority is an indication of how much of the user’s valuable attention should be consumed by this notification. Low-priority notifications may be hidden from the user in certain situations, while the user might be interrupted for a higher-priority notification. The system sets a notification’s priority based on various factors including the setPriority value.
    The effect may differ slightly on different platforms.

Finally the whole source code for the MainActivity::class

 


 

Follow and like us:
Click to rate this post!
[Total: 0 Average: 0]

Leave a comment

stay informed!

Subscribe to receive exclusive content and notifications