Create Notification in Android

Notifications

A notification is a message you can display to the user outside of your application’s normal UI. When you tell the system to issue a notification, it first appears as an icon in the notification area. To see the details of the notification, the user opens the notification drawer. Both the notification area and the notification drawer are system-controlled areas that the user can view at any time.

Building Notification

Screenshot_20160308-130922

 

Build Intent

/**
 * Create an intent that will be fired when the user clicks the notification.
 * The intent needs to be packaged into a {@link android.app.PendingIntent} so that the
 * notification service can fire it on our behalf.
 */
Intent intent = new Intent(Intent.ACTION_VIEW,
  Uri.parse("http://developer.android.com/reference/android/app/Notification.html"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

Setup Notification Builder

To create a notification with the support library, you create an instance of NotificationCompat.Builder and issue the notification by passing it to notify().
/**
 * Use NotificationCompat.Builder to set up our notification.
 */
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
		.setSmallIcon(R.drawable.ic_stat_notification)
		.setContentTitle("Basic Notification")
		.setContentIntent(pendingIntent);

Get an instance of the NotificationManager service

/**
 * Here we instantiate the Notification Manager object to start/stop the notifications
 */
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

Build the notification and issues it with notification manager.

notificationManager.notify(notificationId, notificationBuilder.build());
Note: notificationId is a numeric value that uniquely identifies the notification that we'll be sending
Advertisements

One thought on “Create Notification in Android

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