Notification implementation for Android


Implementing notifications in Android is simple enough to do. Notifications give your app more richness and you can display messages or other information, even a couple tasks/buttons.

Here’s a very simple notification implementation that consists of simple text and an icon.

public void addNotification(Context context) 
{
	NotificationCompat.Builder builder =
	         (android.support.v7.app.NotificationCompat.Builder) new NotificationCompat.Builder(context)
		 .setSmallIcon(R.drawable.notif_icon) //a small image/icon to display with our notification
		 .setContentTitle("Notification")
		 .setContentText(Calendar.getInstance().getTime());
	builder.setAutoCancel(true);
	Intent notificationIntent = new Intent(context, MainScreen.class);
	PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
	   PendingIntent.FLAG_UPDATE_CURRENT);
	builder.setContentIntent(contentIntent);

	// Add as notification
	NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
	manager.notify(0, builder.build());
	return;
}

You can also add action buttons to your notifications, see here: http://www.vogella.com/tutorials/AndroidNotifications/article.html#example-notificationmanager

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