
What is Notification in Android ?
Notification is a message you can display to the user outside of your application’s normal UI. It is provides short, timely information about the action in the application. it is not running.
Notification are displays the icon, title and some amount of the content text.
Notification is used to NotificationManager Class.Notification is a Alert of Message to user and it is short time display information of user. It is by default removed to use of swipe to removed notification.
Notification Set the use of Properties of NotificationCompat.Builder. it is used to set the icon,title,description in Notification.And create the PendingIntent is used to set the Flag of Notification and then builder to set content Intent of pendingIntent object.
NotificationCompat.Builder class allows easier control over all the flags, as well as help constructing the typical notification layouts.
Properties Of Notification in Android
setSmallIcon(): It is Set the Icon of Notification.
setContentTitle() : It is Set the Title of Notification.
setContentText() : It is used to Set the Text of Notification.
setAutoCancel(): It is used to Cancelable Notification.
setPriority(): It is used to Set the Priority of Notification.
Example of Notification in Android
Create activity_main.xml File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.bhaumik.notificationdemo.MainActivity"> <Button android:id="@+id/btn_click" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="Click To See Notification"/> </RelativeLayout> |
Create MainActivity.java File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package com.example.bhaumik.notificationdemo; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.app.NotificationCompat; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.btn_click); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(MainActivity.this) .setContentTitle("Notification Example") .setContentText("You hv Receive New Message..."); Intent intent = new Intent(MainActivity.this,MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent); NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(0,builder.build()); } }); } } |
Above The Example of Create Simple Notification When you can click the button to display notification. Set the Title Description and icon in the notification of Android.