Saturday, 7 November 2015

Showing notification from you Android App


Many time you must have seen apps send out notifications that you see in the title bar of your screen. For eg. when you receive a SMS or when your battery is low. Android provides mechanism for your app to send out such notifications. In this post we will see how to do it programmatically.

Showing notification from you Android App

Here is what we are going to do. We will simply create a button that when on click will show a notification and close the App. On click of that notification we will restart our App.

Before we write our Activity code create a simple layout with button at the center. To see more details on centering your layout refer -

PATH : NotificationDemo\app\src\main\res\layout\activity_main.xml

<RelativeLayout xmlns:android=""
    xmlns:tools="" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context=".MainActivity">

        android:text="Show Notification"


Next lets write our Activity class - 

public class MainActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        Button myButton = (Button) findViewById(;
        myButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                Intent intent = new Intent(MainActivity.this, MainActivity.class);
                PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this, (int) System.currentTimeMillis(), intent, 0);

                Notification notification  = new Notification.Builder(MainActivity.this)
                        .setContentTitle("Test Notification")
                        .setContentText("You have received a notification from OSFG")
                        .addAction(R.mipmap.ic_launcher, "Action1", pIntent)
                        .addAction(R.mipmap.ic_launcher, "Action2", pIntent).build();

                NotificationManager notificationManager =
                        (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

                notificationManager.notify(0, notification);

Now lets test out our code. Install the App on your Android phone or VM and launch it. You should see following screen -

Now click on Show notification button. You should receive a notification in the title bar and your App should get close.

Now look at the code again and relate the subject title and actions we had set. Also notice that we are calling finish() on showing notification that will effectively terminate our main activity. Now click on the notification (or the actions in it). Our App should get launched again. You should see activity with show notification button again.

NOTE : You can call the cancel() for a specific notification ID from the NotificationManager. You can also do cancelAll() method that will essentially removes all of the notifications you had previously issued.

NOTE : You can also use NotificationCompat to get builder. You can use things like NotificationCompat.Builder#setLargeIcon(Bitmap) to allow you to take full advantage of Android 3.0+ with things like the large icon, while maintaining compatibility on versions of Android prior to 3.0 that do not support such thing.

What is this pending intent?

If you are still wondering what is this intent and how is it different than a normal intent then you are on right track. PendingIntent is executed by remote component (Like NotificationManager,alarm manager or other 3rd party applications) with the same permissions as that of the component that hands it over (The one that creates the notification).

 NOTE :  To perform a broadcast via a pending intent you can get a PendingIntent via the getBroadcast() method of the PendingIntent class and to perform an activity via a pending intent, you can get the activity via PendingIntent.getActivity() method.

Disabling notifications

Some apps provide settings in their App itself to turn off notifications. If setting is not provided you can also disable it from App info screen. For more details refer one of the earlier posts - 

Related Links

How To Add Google+ Follower Widget in your website or WordPress/Blogger blogs


It been almost two and a half years since I am blogging on bloggers and I though it's time I see what wordpress has to offer. To surprise the UI is kind of complex and have limited widget support. Also it does not allow you to add plugins. After a few customizations I decided to add a Google+ followers widget but there unfortunately isn't one. Hence this post. It's a simple html/javascript. So you can plug it into any website. Bloggers as you know already has a widget for this.

How To Add Google+ Follower Widget in your blog/website

  • First create a simple text/html widget
  • Next add following javascript code to it
<div class="g-plus" data-action="followers" data-height="300" data-href="" data-source="blogger:blog:followers" data-width="320">
<script type="text/javascript">
      (function() {
        window.___gcfg = {'lang': 'en'};
        var po = document.createElement('script');
        po.type = 'text/javascript';
        po.async = true;
        po.src = '';
        var s = document.getElementsByTagName('script')[0];
        s.parentNode.insertBefore(po, s);

  • Next Save and Publish. You should now be able to see the widget on your wordpress site.
NOTE : Make sure you use your own G+ page URL as the value of data-href attribute in div tag.

You should see it get rendered as follows -

t> UA-39527780-1 back to top