Android Notification Example Code

What We’ll Be Making

The pictures below shows the result of this tutorial. When the user clicks the “Notify Me” button a notification pops up showing various text parameters specified by the programmer.

Android Notification

Android notification displaying ticker text, notification title, and notification content

The App’s Interface: activity_main.xml

For the app’s GUI a simple button will suffice.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Notify Me" />

</RelativeLayout>

Activity Code

Here is the activity’s code. Scroll below for an explanation.

package com.example.notificationexample;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{

	private Button b;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b=(Button)findViewById(R.id.button1);
        b.setOnClickListener(this);
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

	@Override
	public void onClick(View v) {
		Intent intent = new Intent();
		PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
		Notification noti = new Notification.Builder(this)
		.setTicker("Ticker Title")
		.setContentTitle("Content Title")
		.setContentText("Notification content.")
		.setSmallIcon(R.drawable.ic_launcher)
		.setContentIntent(pIntent).getNotification();
		noti.flags=Notification.FLAG_AUTO_CANCEL;
		NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		notificationManager.notify(0, noti); 
	}
}

Code Explanation

The notification class requires an intent, this is done with the following lines of code.

	Intent intent = new Intent();
	PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

Using the new “Notification.Builder” method a notification object is made. This is where we specify the ticker text, notification title, content, and icon to be displayed. There are many more options available, check them out!

		Notification noti = new Notification.Builder(this)
		.setTicker("Ticker Title")
		.setContentTitle("Notification Content Title")
		.setContentText("Notification content.")
		.setSmallIcon(R.drawable.ic_launcher)
		.setContentIntent(pIntent).getNotification();

The notification flags control the behavior of the notification, for example do you want your phone to flash when a notification is called? In our case the only flag I used was auto cancel flag which closes the notification when the user clicks it, yes, if we did not have this flag the notification would not close, it’s weird that this isn’t default right?

		noti.flags=Notification.FLAG_AUTO_CANCEL;

Lastly, using the notification manager we call the notification. The first parameter of the notificationManger.notify method is a unique ID for the notification within your application, the second parameter is the notification object we created using the builder method.

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti); 

Related posts: