Notifications in Android applications are very useful. This tutorial explains how to set notification in your application with the help of example.
Notification
Notification is to inform or notify about something. Notification in Android apps are very helpful. It gives information about something important. Now a day’s user want ease of use, they don’t want to open apps for new updates again and again. Instead of this, they enable push up notifications. For example no one wants to check his emails again and again in a day, it’s better to tuned on notifications when he receives a new mail. To fulfill this demand of notification this tutorial provides an easy example to set notifications.
NotificationCompat.Builder
NotificationCompat builder class help us to develop notification layout. It has many methods used for notification in application. Here are few of them:
- addNotification(NotificationCompat.Action action): As the name shows this method is used to add action to notification.
- addPerson( String uri): This method is used to add person to the notification. For example email from a particular person.
- getNotification(): Used to get notification.
- build(): Used to build a notification.
- setCategory( String category): This method is used to set the category of notification.
- setColor( int argb): This method is used to set color of notification.
- setContentTitle( CharSequence title): This method is used to set title of the notification bar.
- setContentText(CharSequence text): This method is used to set the text shown in notification bar.
Notification Manager
Notification manager is a class used to tell the user that something has happened in the background. It informs about events.
Example of Notifications in Android
Let’s start creating a simple example that shows how to use notifications in your Android app. Open your Android Studio and create main activity. In main activity there is just a text view, an image view and button. When user will click on button it will show notification in status bar. Here is the XML layout of activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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="MainActivity"> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="48dp" android:text="Java Tutorial " android:textColor="@android:color/holo_red_dark" android:textSize="30dp" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButton" android:src="@drawable/message" android:layout_below="@+id/textView2" android:layout_centerHorizontal="true" android:layout_marginTop="42dp" /> <Button android:id="@+id/button" android:layout_width="150dp" android:layout_height="wrap_content" android:layout_below="@+id/imageButton" android:layout_centerHorizontal="true" android:layout_marginTop="62dp" android:background="@android:color/holo_red_dark" android:text="Notification" android:onClick="notification" android:textColor="@android:color/background_light" /> </RelativeLayout>
Now create another layout for notifications, here is the code for activity_notification_bar.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="400dp" android:text="You have a new unread message...." /> </LinearLayout>
Here is the NotificationBar.java
package com.example.admin.androidnotifications; import android.os.Bundle; import android.app.Activity; public class NotificationBar extends Activity{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notification_bar); } }
Now open your MainActivity.java and paste the following code
package com.example.admin.androidnotifications; import android.os.Bundle; import android.app.Activity; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.support.v4.app.NotificationCompat; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void notification(View view) { addNotification(); } private void addNotification() { NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.message) .setContentTitle("Unread Message") //this is the title of notification .setColor(101) .setContentText("You have an unread message."); //this is the message showed in notification Intent intent = new Intent(this, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(contentIntent); // Add as notification NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(0, builder.build()); } }
Now run your app, here is the output
After clicking notification button, a new notification will appear in the status bar. Here is how it looks
You can download this project by clicking this link.
Very interesting but what about notifications like whatsapp of other socials do?. In Android 8 background services are nor grated to work. What is the mechanism this social use for notifications.