Notification for android

A notification is a message you can display to the user outside of your application’s normal UI. When you tell the system to issue a notification, it first appears as an icon in the notification area.In order to see the details, the user opens the notification drawer. Both the notification area and the drawer are system-controlled areas that the user can view at any time more can be found here.
We can update the current notification shown or create a new one . These two things can be achieved very easily.
The NotificationManager require a key for starting, if we use the same key over and over it updates the current notification with the same key.
In the case of using another key for NotificationManager it creates new notification instance. Like the figure.
It also contains additional items. Sound (default or assigned tone),Vibration (control duration and pattern of vibration),Light(color and blinking delay are controlled ) and Time (display given time) are among it
Creating the object
Notification mNotification = new NotificationCompat.Builder(this)
This code creates the object, Next part is the tittle ticker. Ticker is created using .setTicker("Ticker heading")
. Ticker is the heading that displays along with the icon in notification area.
Below is an example method for notification.
public void singleNotification(){ Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pi = PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT); //for vibration longs=new long[] { 1000, 1000}; //for led color color=Color.BLUE; Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.InboxStyle inboxStyle =new NotificationCompat.InboxStyle(); inboxStyle.setBigContentTitle("Logicchip"); // Moves events into the expanded layout arraySingleList=new ArrayList(); arraySingleList.add("logicchip a"); arraySingleList.add("logicchip b"); arraySingleList.add("logicchip c"); for (int i=arraySingleList.size(); i >0; i--) { inboxStyle.addLine(arraySingleList.get(i-1).toString()); } Bitmap largeIcon = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher); Notification mNotification = new NotificationCompat.Builder(this) .setTicker("Ticker logicchip") .setContentTitle(String.valueOf(keyForSingleNotification)+" Content Tittle Logicchip") .setLargeIcon(largeIcon) .setContentText("Content Text Logicchips") .setContentInfo(String.valueOf(arraySingleList.size())) .setSmallIcon(R.mipmap.ic_launcher)//icon .setContentIntent(pi) .setVibrate(longs) //for vibration set pattern time .setNumber(arraySingleList.size()) //showing number .setSubText(String.valueOf(arraySingleList.size())+" New updates")//for single line text .setLights(color,2000, 2000)//led blinking .setSound(uri)//for notification tone .setStyle(inboxStyle)//for changing updates .setAutoCancel(true) .setWhen(System.currentTimeMillis())//showing time .build(); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notificationManager.notify(keyForSingleNotification++, mNotification); }
The intent is for displaying the result, It can be explained. When we click the notification it opens the activity mentioned in PendingIntent. Each notification can only open one activity.
The next method shows the updating notification.
public void showNotification() { Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pi = PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT); //for vibration longs=new long[] { 1000, 1000}; //for led color color=Color.BLUE; Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.InboxStyle inboxStyle =new NotificationCompat.InboxStyle(); inboxStyle.setBigContentTitle("Logicchip"); // Moves events into the expanded layout arrayList.add("logicchip "+arrayList.size()); for (int i=arrayList.size(); i >0; i--) { inboxStyle.addLine(arrayList.get(i-1).toString()); } Bitmap largeIcon = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher); Notification mNotification = new NotificationCompat.Builder(this) .setTicker("Ticker logicchip") .setContentTitle("Content Titile Logicchip") .setLargeIcon(largeIcon) .setContentText("Content Text Logicchips") .setContentInfo(String.valueOf(arrayList.size())) .setSmallIcon(R.mipmap.ic_launcher)//icon .setContentIntent(pi) .setVibrate(longs) //for vibration set pattern time .setNumber(arrayList.size()) //showing number .setSubText(String.valueOf(arrayList.size())+" New updates")//for single line text .setLights(color,2000, 2000)//led blinking .setSound(uri)//for notification tone .setStyle(inboxStyle)//for changing updates .setAutoCancel(true) .setWhen(System.currentTimeMillis())//showing time .build(); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notificationManager.notify(keyForNotification, mNotification); }
you can see by comparing the two methods keyForNotification
and keyForSingleNotification++
. Where keyForNotification
is for updating notifications and keyForSingleNotification++
is for individual notifications.
The full code is displayed below with xml file.
MainActivity.java
import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; import android.media.RingtoneManager; import android.net.Uri; import android.os.Bundle; import android.support.design.widget.CoordinatorLayout; import android.support.design.widget.Snackbar; import android.support.v4.app.NotificationCompat; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.widget.Button; import java.util.ArrayList; public class MainActivity extends AppCompatActivity implements View.OnClickListener,Runnable { private CoordinatorLayout coordinatorLayout; private ArrayList arrayList=new ArrayList(),arraySingleList; private int keyForNotification=0,keyForSingleNotification=0; private long[] longs; private int color; private Thread thread; private Button buttonThread,buttonSingle,buttonMulti; private boolean oN=false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); buttonThread=(Button)findViewById(R.id.buttonThread); buttonSingle=(Button)findViewById(R.id.buttonSingle); buttonMulti=(Button)findViewById(R.id.buttonMulti); buttonThread.setOnClickListener(this); buttonSingle.setOnClickListener(this); buttonMulti.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.buttonThread: if(oN){ thread=null; oN=false; buttonThread.setText("start"); }else { thread = new Thread(this); thread.start(); oN=true; buttonThread.setText("stop"); } break; case R.id.buttonSingle: singleNotification(); break; case R.id.buttonMulti: showNotification(); break; } } public void showNotification() { Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pi = PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT); //for vibration longs=new long[] { 1000, 1000}; //for led color color=Color.BLUE; Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.InboxStyle inboxStyle =new NotificationCompat.InboxStyle(); inboxStyle.setBigContentTitle("Logicchip"); // Moves events into the expanded layout arrayList.add("logicchip "+arrayList.size()); for (int i=arrayList.size(); i >0; i--) { inboxStyle.addLine(arrayList.get(i-1).toString()); } Bitmap largeIcon = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher); Notification mNotification = new NotificationCompat.Builder(this) .setTicker("Ticker logicchip") .setContentTitle("Content Titile Logicchip") .setLargeIcon(largeIcon) .setContentText("Content Text Logicchips") .setContentInfo(String.valueOf(arrayList.size())) .setSmallIcon(R.mipmap.ic_launcher)//icon .setContentIntent(pi) .setVibrate(longs) //for vibration set pattern time .setNumber(arrayList.size()) //showing number .setSubText(String.valueOf(arrayList.size())+" New updates")//for single line text .setLights(color,2000, 2000)//led blinking .setSound(uri)//for notification tone .setStyle(inboxStyle)//for changing updates .setAutoCancel(true) .setWhen(System.currentTimeMillis())//showing time .build(); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notificationManager.notify(keyForNotification, mNotification); } public void singleNotification(){ Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pi = PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT); //for vibration longs=new long[] { 1000, 1000}; //for led color color=Color.BLUE; Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.InboxStyle inboxStyle =new NotificationCompat.InboxStyle(); inboxStyle.setBigContentTitle("Logicchip"); // Moves events into the expanded layout arraySingleList=new ArrayList(); arraySingleList.add("logicchip a"); arraySingleList.add("logicchip b"); arraySingleList.add("logicchip c"); for (int i=arraySingleList.size(); i >0; i--) { inboxStyle.addLine(arraySingleList.get(i-1).toString()); } Bitmap largeIcon = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher); Notification mNotification = new NotificationCompat.Builder(this) .setTicker("Ticker logicchip") .setContentTitle(String.valueOf(keyForSingleNotification)+" Content Tittle Logicchip") .setLargeIcon(largeIcon) .setContentText("Content Text Logicchips") .setContentInfo(String.valueOf(arraySingleList.size())) .setSmallIcon(R.mipmap.ic_launcher)//icon .setContentIntent(pi) .setVibrate(longs) //for vibration set pattern time .setNumber(arraySingleList.size()) //showing number .setSubText(String.valueOf(arraySingleList.size())+" New updates")//for single line text .setLights(color,2000, 2000)//led blinking .setSound(uri)//for notification tone .setStyle(inboxStyle)//for changing updates .setAutoCancel(true) .setWhen(System.currentTimeMillis())//showing time .build(); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notificationManager.notify(keyForSingleNotification++, mNotification); } @Override public void run() { Thread thisThread = Thread.currentThread(); while (thread == thisThread) { try { Thread.sleep(3000); showNotification(); } catch (InterruptedException e) { e.printStackTrace(); } } } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/content_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" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="logicchip.blog_6_notifiaction.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center" android:layout_centerVertical="true" android:layout_centerHorizontal="true"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="start" android:id="@+id/buttonThread" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Single" android:id="@+id/buttonSingle" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Multi" android:id="@+id/buttonMulti" /> </LinearLayout> </RelativeLayout>
You must add permission for VIBRATION. Add this to the AndroidManifest.xml file.
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
Don’t hesitate to comment below if you have any questions or doubts.