Friday, 20 May 2016

Android Notification Example | Android notification tutorial

Hey warriors whats going on guys , hope you all are doing great in android since in all previous post we were focusing on advance topics of android but Now I have decided to complete all basic tutorial for beginners. So in this post I am going to teach you how to create simple as well as advance Notification in android ,Notification is beauty of android app ,think your app is not running in foreground and you want to notify your user for any reason what will you do ?? of course you will use notification man.


 According to android developer page
 "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. To see the details of the notification, the user opens the notification drawer. Both the notification area and the notification drawer are system-controlled areas that the user can view at any time" .

So lets start with coding , like every post i will make module of steps and we will follow them to complete this tutorial.

  • Create a New project in Android studio File>>New>>New project.
  • Add three button in to activity_main.xml.
  • Initialize all three button and an Instance of NotificationManager   in MainActivity.java.
  • Create method for all three type of notification.
  • Call each notification on different button click.

1. Create a New project in Android studio Go to File>>New>>New project like below picture.


2.Open your activity_main.xml and add three button for open all three type of notification. here is my activity_main.xml.

3.Initialize all three button and an Instance of  NotificationManager   in MainActivity.java. Notification manager take instance of Notification builder and launch notification, here is my code to initialize all button and notification manager. I am also adding clcikListener to all three button so it will ask you to implement onClickListener so implement in MainActivity.java .

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    Button btn_not_simple, btn_not_expand, btn_not_btn;
    private  NotificationManager mNotificationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mNotificationManager=(NotificationManager) this.getApplicationContext().getSystemService(this.NOTIFICATION_SERVICE);
        btn_not_simple= (Button) findViewById(R.id.btn_not_simple);
        btn_not_expand= (Button) findViewById(R.id.btn_not_expand);
        btn_not_btn= (Button) findViewById(R.id.btn_not_btn);

        btn_not_simple.setOnClickListener(this);
        btn_not_expand.setOnClickListener(this);
        btn_not_btn.setOnClickListener(this);

    }


4.Create method to generate notification, Since i will post code for all three type but i will discuss only one type of notification in detail. I am going to discuss How to create simple notification in Android, So here is my method createSimpleNotification().

 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
 void simpleNotification(){

        Intent resultIntent = new Intent(this, MainActivity.class);

 // Creating a artifical activity stack for the notification activity
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(MainActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);

        // Pending intent to the notification manager
        PendingIntent resultPending = stackBuilder
                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        // Building the notification
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher) // notification icon
                .setContentTitle("I'm a simple notification") // main title of the notification
                .setContentText("I'm the text of the simple notification") // notification text
                .setContentIntent(resultPending); // notification intent

        // mId allows you to update the notification later on.
        mNotificationManager.notify(10, mBuilder.build());
    }


In above method you can see first of all i have created a intent instance , this intent will be used when you will click on notification then intent will call MainActivity.java class, you can call any activity according to need.

Next i am creating instance of TaskStackBuilder , By using TaskStackBuilder I am Creating a artifical activity stack for the notification activity The stack builder object will contain an artificial back stack for thestarted Activity.This ensures that navigating backward from the Activity leads out of your application to the Home screen.

Next i am creating Pending intent using stack builder.After that using NotificationCompat i am creating notification using following methods:
  • setSmallIcon(): used to set icon of notification
  • setContentTitle(): used to set Title of notification
  • setContentText(): used to set actual text of notification
  • setContentIntent(): used to add pending intent to notification.

After all this using Notification manager launch notification.

So as discussed above i am not going to explain each type of notification but i have included all type in code, here is my MainActivity.java


you can download sample code from below button.if this post was helpful for you don't forget to give your feedback in comment because its motivate me to write new post.


https://codeload.github.com/kundansviet/NotificationMaster/zip/master

0 comments:

Post a Comment