Overview
Notifications have a very important role in any mobile development application. It is a faster way of communication to provide information to the user. Today, we get notifications for almost every time when a user does any kind of transaction, either on social media apps or any financial/services related apps.
Before starting a discussion on notifications, below are some useful links to start programming in Android applications from scratch.
- Android Programming - Day One
- Android Programming - Day Two
- How To Display A Dialog Windows In Android
- How To Display A Progress Dialog Window In Android
- Intent In Android
- Return Data Using Intent Object In Android Applications
- Passing Data Using An Intent Object In Android Applications
- Fragments In Android Applications
- Adding Fragments Dynamically In Android Application
- Fragment Life Cycle In Android Application
- Interaction Between Two Fragments
- Calling In-Built Functions in Android Application
- Intent Object and Intent Filters in Android Application
Introduction
You have already aware of using the Toast class to display a message to the user. This class is a handy way to show user alerts, it is not persistent. It flashes on the screen for a few seconds and then disappears. Users may easily miss important information if they are not looking at the screen.
Messages are more important, so we need to use a more persistent method. For this, you can use the NotificationManager class to display a persistent message at the top of the device which is known as a status bar.
Code
Create a new project named ShowNotification.

Create a blank activity called MainActivity.

Add a new blank activity.

And name it DisplayNotification.

Add code in this class
- package com.example.administrator.shownotification;
- import android.app.NotificationManager;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- public class DisplayNotification extends ActionBarActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_display_notification);
- //Look up the notification manager service
- NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
- // Cancel the notification that we started
- notificationManager.cancel(getIntent().getExtras().getInt("NotificationID"));
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_display_notification, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
Add <intent-filter> and <uses-permisson> in the AndroidMenifest.xml file
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.administrator.shownotification"
- android:versionCode="1"
- android:versionName="1.0"
- >
- <uses-sdk android:minSdkVersion="20"/>
- <uses-permission android:name="android.permission.VIBRATE"/>
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name=".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=".DisplayNotification"
- android:label="@string/title_activity_display_notification" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN"/>
- <category android:name="android.intent.category.DEFAULT"/>
- </intent-filter>
- </activity>
- </application>
- </manifest>



Roshan GuragainPosted Oct 16, 2016, 6:42 AM
Nice..
Vignesh ManiPosted Aug 17, 2016, 7:51 AM
Nice
kalu singh raoPosted Aug 17, 2016, 1:59 AM
Nice share