Online Tutorials & Training Materials | STechies.com
Register Login
Advertisement

Notification Manager in Android

|| 0

Notification Manager in Android
Stechies

Building a Notification in Android

Below is an app which has one button  'Notify Me' and when we click this button, it’s going to give us a notification as shown in the image below.

Now when we drag the notification down, we will see that this notification has content title, notification content and some extra features which we are going to see in this tutorial.

Please follow the steps below in order to use notification manager in Android:

Step 1) So first of all in a blank project we are going to use a button here and going to change the button text as 'Notify Me' and leave the button id as default.

Step 2) Now go to mainactivity.java file and declare a button variable and give alt+enter to include the button imports

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.menu;
import android.view.MenuItem;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {
Button btn;
@Override

Step 3) Now take this button variable and cast it inside my onCreate method.

@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
btn = (Button) findViewById (R.id.button);

Step 4) Now after casting this button, we can use the button object and we can call a setOnClick method on it and inside this setOnClickListener, we are going to call new onClickListener.

btn.setOnClickListener(
      newView.OnClickListener() {
          @Override
       public void onClick(View  v) {

Step 5) Now inside this onClickMethod, first of all, we will create an object of intent.

@Override
public void onClick(View  v) {
Intent intent=new Intent ();
}

Step 6) Now I am going to create an object of pending intent and then call getactivity. 

  • The first argument which it takes is the activity itself which is mainactivity.this
  • The second argument is 0,
  • The third argument is your intent
  • The fourth argument is 0

PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0

Note: Pending intent is a token that you can give to another application, for example, notificationmanager which we are going to use in our case and this will allow this application to use the permission of your application to execute a predefined piece of code.

Step 7) Now we are going to create our notification object and include our import and then you can add some attribute to this object and then you can set some more properties like setcontenttitle and setcontenttext and then we will add .setcontenttext and this is the text for your content we can add as many texts as we want. So after this, we will add our icon, for example you can take it from your drawable so you can go to your res folder and in here you have your drawable folder and in there you have some default pictures. If you want, you can add your own custom picture for your notification but for now, we using the default pictures. At last, you can add the content so .setcontent and we will pass our pintent here and then we will call getnotification on this notification object

Notificat‭ion noti = new Notification.Builder(MainActivity.this)
          .setTicker("TickerTitle")
          .setContentTitle("Content Title")
          .setContentText("Content Text sfglkfdgklfd sadgjfsdg sdaghfsg o")
          .setSmallIcon(R.drawable.ic_launcher)
          .setContentIntent(pIntent).getNotificattion();

Step 8) Now in the next line we can add some flags to our notifications. There will be so many flags which we can add here but the flag we want is flag_auto_cance. This flag will cancel our notification whenever we click the notification.

noti.flags = Notification.FLAG_AUTO_CANCEL;

Step 9) Now in the next line, we will declare our notificationmanager object and then cast notificationmanager and then we will get services, so getsystem service and in here, we want to call notification service and then we are going to take our notificationmanager and then we will call out notify method on it. So just take your notificationmanager object .notify and the first argument will be 0 and second argument is your notification object.

noti.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nm.notify(0,noti);

Now our program is running therefore when we click this Notify Me button, it’s going to give us a notification and when we drag this notification, we will see the content title, content text and when we click this, this notification is gone.

Now in order to add some more features to your notifications, something like these icons and some more content or some more actions, what we need to do is go to our code and call some more method like .addaction and the first argument it takes is the icon. The second argument is the text, for example ‘action 1’ and the third argument is your pending content object (pIntent). In the same way, you can add as many actions as you want, just paste it below this and add any number of arguments.

.addAction(R.drawable.ic_launcher,"Action 1",pIntent)
.addAction(R.drawable.ic_launcher,"Action 2",pIntent)
.setContentIntent(pIntent).getNotification();

Now when we run the program and click this notify me button, we see the notify icon and in there, we will see action 1 and action 2.

 


Related Articles

Advertisement
Advertisement
Advertisement