Android 4.1 Jelly Bean Notification Tutorial Part III


In Android 4.1 Jelly Bean Notification Tutorial Part 1 & Part 2 we saw how to create simple notification and how to create styled notifications.

In this post, we will see how to add an action button on notifications and how they exactly look on screen when added.

Add action requires 3 parameters, an icon to be displayed, a text to be displayed beside the icon and an pending intent to be fired when button is clicked.

Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 01, intent, Intent.FLAG_ACTIVITY_CLEAR_TASK);

Bitmap smallBitmap = null;
BufferedInputStream buf = null;

try {
	buf = new BufferedInputStream(getAssets().open("me.jpeg"));

	// Create the bitmap to be set in notification.
	smallBitmap = BitmapFactory.decodeStream(buf);
	buf.close();
} catch (Exception ex) {
	ex.printStackTrace();
}

{
	Builder builder = new Notification.Builder(this);
	builder.setSmallIcon(R.drawable.ic_launcher);
	builder.setTicker("3 new messages from Chitranshu");
	builder.setContentTitle("3 new messages from Chitranshu");
	builder.setContentText("+ 5 more");
	builder.setSubText("Click here to go to inbox.");
	builder.setLargeIcon(smallBitmap);
	builder.setAutoCancel(true);
	builder.addAction(android.R.drawable.sym_action_email,"Read Emails", pendingIntent);
	((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(00, builder.build());
}

 

In the screen shot you can see that display of notification action button requires extra display area. The whole notification looks bigger than usual.

But just like styled notifications, the action notification also looses its extended display area when it get pushed down from first position.

Therefore we will also have to setDeleteIntent() which will make sure that our application gets the event whenever the notification is clicked.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s