Android 4.1 JellyBean Notifications Code example [Tutorial]

July 19, 2012 | Author:

Share

If you’re an an Android developer, by now you must started upgrading your application to run on Android 4.1 Jellybean.

Among the several popular additions to Jellybean, one that users would love instantly is the new revamped Notifications system.

There are 3 new classes Notification.InboxStyle, Notification.BigPictureStyle & Notification.BigTextStyle which which helps us in creating notifications with larger content area & picture review.

Lets get it rolling with Few examples of Code that bring you to speed with JellyBean. You can create a notification using NotificationManager, like you always used to:

Original way of Pushing simple notifications in Android:

/**
	 * Push a simple notification (old way)
	 */
	private void pushSimpleNotification() {
		Notification notification = new Notification(
				android.R.drawable.ic_menu_camera, "Hello camera!",
				System.currentTimeMillis());
		notification.flags |= Notification.FLAG_AUTO_CANCEL;
		notification.number += 1;

		Intent intent = new Intent(this, MainActivity.class);
		PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
		notification.setLatestEventInfo(this, "title", "content", activity);
		notificationManager.notify(0, notification);
	}

Big Picture in Notification Area can be shown with the following code:

	/**
	 * Big picture notification!
	 */
	public void pushBigPictureNotifications() {
		Builder build = new Notification.Builder(this)
				.setContentTitle("You got mail")
				.setContentText("email subject")
				.setTicker("This email has photo")
				.setSmallIcon(R.drawable.ic_action_search)
				.addAction(
						android.R.drawable.ic_btn_speak_now,
						"Speak!",
						PendingIntent.getActivity(getApplicationContext(), 0,
								getIntent(), 0, null))
				.addAction(
						android.R.drawable.ic_dialog_map,
						"Maps",
						PendingIntent.getActivity(getApplicationContext(), 0,
								getIntent(), 0, null))
				.addAction(
						android.R.drawable.ic_dialog_info,
						"Info",
						PendingIntent.getActivity(getApplicationContext(), 0,
								getIntent(), 0, null));

		Notification notification = new Notification.BigPictureStyle(build)
				.bigPicture(
						BitmapFactory.decodeResource(getResources(),
								R.drawable.jellybean)).build();

		notificationManager.notify(0, notification);
	}

Big Text Notification can be shown with this code:

	/**
	 * Big notifications
	 */
	public void pushBigTextNotification() {
		Builder builder = new Notification.Builder(this)
				.setContentTitle("New mail from me").setContentText("subject")
				.setSmallIcon(android.R.drawable.ic_menu_agenda);
		Notification notification = new Notification.BigTextStyle(builder)
				.bigText("This Notification can actually get very big and still contain all of the text. It's amazing how Jelly Bean notifications work!")
				.build();
		notificationManager.notify(0, notification);
	}

Gmail like Inbox multiple notifications collapsed into one area:

	/**
	 * Inbox notifications
	 */
	private void pushInboxNotifications() {
		Builder builder = new Notification.Builder(this)
				.setContentTitle("10 New emails for me")
				.setContentText("subject")
				.setSmallIcon(android.R.drawable.ic_menu_agenda);

		Notification notification = new Notification.InboxStyle(builder)
				.addLine("line1 text")
				.addLine("line2 text")
				.addLine("line3 text")
				.setSummaryText("+7 more")
				.build();
		notificationManager.notify(0, notification);
	}

Follow us on Google+, Twitter, Facebook for latest in Android mods, news.

Comments

blog comments powered by Disqus