Notification

A notification is a message you can display to the user outside of your application’s normal UI(short message displayed on the status line).It typically announces the happening of an special event for which a trigger has been set.After opening the Notification panel the user may choose to click on  a selection and execute an associated activity.

The aim of the notifications is to keep the user informed about events or alerts in a more persistent method, as the user can view them at any time

notification_area

 

Notification Manager

Android allows to put notification into the titlebar of your application. The user able to expand the

notification bar and by selecting the notification the user can trigger another activity.

Notification can  take  different forms:

1.A presisent icon that goes  in the status bar and is accessible through the launcher

2.Turning on or flashlight LED’s on the device

3.Alerting the user by flashing the backlight,playing a sound.

Retrieve the class through getSystemService(String).

Example:

String serviceName = Context.NOTIFICATION_SERVICE;

notificationManager = (NotificationManager)getSystemService(serviceName);

This class represents how a persistent notification is to be presented to the user using NotificationManager.

Must see : Top 5 Best Feature in Android Nougat

Public Notification(int icon,CharSequence tickerText,long when)

Parameters

Icon – The resource id of the icon to put in the status bar.

tickerText – The text that flows by in the statusbar when the notification first activates.

When – The time to show in the time field.In the System.currentTimeMillis timebase.

Notification Methods

1.Public void notify(int id,Notification notification) – This method post a notification to be shown in the status bar.

Parameters

Id – An identifier of this notification unique within your application

Notification – A Notification object describing how to notify the user other than the view you are providing.It must not be null.

status-bar2

 

2.Public void setLatestEventInfo(Context context,CharSequence contentTitle,CharSequence contentText,PendingIntent contentIntent)

Parameters

context – The context of your application or activity.

contentTitle – The title that goes in the expanded entry

contentText – The text that goes in the expanded entry

contentIntent – The intent to launch when the user click the expanded notification.

Call this setLatestEventInfo() Method of the Notification class and pass the pending intent along with notification subject details.

Example : Creating a simple notification

Java

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!”);

//Create the intent that’ll fire when the user taps the notification//

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(“http://www.androidupdates.com/”));

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

// Gets an instance of the NotificationManager service//

NotificationManager mNotification=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

// If you want to update this notification at a later date, you need to assign it an ID. You can then use this ID whenever you issue a subsequent notification.In this example, the notification’s ID is 001//

NotificationManager.notify().

mNotification.notify(001, mBuilder.build());

}

}

XML

<?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.androidbeginners.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>

Android Mainfest.xml

<?xml version=”1.0″ encoding=”utf-8″?>

<manifest xmlns:android=”http://schemas.android.com/apk/res/android”

package=”com. androidbeginners ” >

<application

android:allowBackup=”true”

android:icon=”@drawable/ic_launcher”

android:label=”@string/app_name”

android:theme=”@style/AppTheme” >

<activity

android:name=”com. androidbeginners.MainActivity”

android:label=”@string/app_name” >

<intent-filter>

<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />

</intent-filter>

</activity>

<activity android:name=”.AppCompatActivity”

android:label=” ”

android:parentActivityName=”.MainActivity”>

<meta-data

android:name=”android.support.PARENT_ACTIVITY”

android:value=”.MainActivity”/>

</activity>

</application>

</manifest>

Android Studio installs the app on your AVD and starts it and if everything is fine with your setup and application, it will display following Emulator window

notification-app-UI-768x1365

 

To trigger the notification, simply give the button a tap, you should see a new ‘Hello World’ notification.

 

notification-768x1365

 

Visit : “Easy Tips to learn Android for Beginners”

Develope your own Android app