Last Updated:

 Notification + PendingIntent in Android | With Example

In this tutorial, we'll learn how to create notifications () using , as well as break down the different settings and customization of our notifications.NotificationsPendingIntent

A quick tour of PendingIntent in Android

First, we need to understand what the object .PendingIntent

And so, PendingIntent is a wrapper for Intent objects and defines an action that should be performed in the future. Typically, it is used in order to trigger notifications or paired with AlarmManager for repetitive actions.

 

For security reasons, the Intent object passed to pendingIntent must explicitly specify the name of the component. Each explicit Intent, in turn, must be handled by components such as Activity, BroadcastReceiver, or Service. To do this, PendingIntent uses the following methods:

  • PendingIntent.getActivity() - Get the PendingIntent to start the start Activity.
  • PendingIntent.getBroadcast() - Get the PendingIntent to run the Broadcast objects.
  • PendingIntent.getService() is the PendingIntent to start the service.

Let's look at the creation of PendingIntent using an example:

Let's take a closer look at the parameters that we passed in the method:getActivity()

  • The first parameter is the context in which PendingIntent starts its work.this
  • The second parameter 1 () is the request code with which we send our PendingIntent.requestCode
  • The third parameter is an explicit intent that specifies the Activity object to run.intent
  • The fourth parameter is one of the possible flags of the PendingIntent object. In the example above, we used , which will update the PendingIntent value (if one already exists) with the last Intent. There is also a flag such as , which cancels the already existing PendingIntent.PendingIntent.FLAG_UPDATE_CURRENTFLAG_UPDATE_CURRENTFLAG_CANCEL_CURRENT

Notifications in Android. Notification Objects

Android already has a class, which is a convenient way to notify the user with various notifications, but the problem is that these messages are not permanent and disappear within a few seconds. Yes, Android currently uses the SnackBar widget for this purpose, but we're talking about Notification objects that imply a different way to notify the user.Toast

With the help of an object, we can show the user a message outside the user interface of our application and use various ways to call it.Notification

Create in Android. Most Used MethodsNotification

Notifications are generated using the class as shown below:NotificationManager

It also provides an interface for creating a notification object:Notification.Builder

Now let's look at the most used methods when creating notifications:

  • Method - Composes all the specified parameters and returns a new Notification object.build()
  • Method – Sets the notification behavior so that it is automatically canceled when the user clicks a button on the panelNotificationCompat.Builder setAutoCancel(boolean autoCancel)
  • The method provides work with RemoteViews (lock screen notifications) instead of the standard notification barsetContent (RemoteViews views)
  • The method sets the text size to a large size on the right side of the notificationsetContentInfo (CharSequence info)
  • The method provides a PendingIntent object to send when the user clicks on the notification.setContentIntent (PendingIntent intent)
  • The method sets the text on the second line of the notificationsetContentText (CharSequence text)
  • Method sets the text in the first line of the notification (setting the notification header)setContentTitle (CharSequence title)
  • The method sets the default alert settings. For example setDefaults (int defaults)mBuilder.setDefaults (Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
  • U methodsets a large icon for notificationsetLargeIcon (Bitmap icon)
  • U methodsets a small icon on the notification barsetSmallIcon (int icon)
  • The u methodsets the text that will be displayed in the status bar when the notification first appearssetTicker (CharSequence tickerText)
  • The method sets the vibration pattern (how long it will last and what intervalswhen the notification first appearssetVibrate (long[] pattern)
  • The method sets the time when the event occurred in the notification (Notification objects are sorted by this time in the notification bar).setWhen (long when)

The interface allows you to add up to three buttons with specific actions per notification.Notification.Builder

Starting with Android API version 4.1 and above, Android has large notifications with advanced functionality.

Cancelable notifications in Android

Android allows us to cancel notifications by calling a method for a specific notification (we get by id) or call a method to delete all previously received notifications.cancel()NotificationManager.cancelAll()

Create a Notification in Android

In our test application, we'll create an Intent to view a specific web page, wrap it in PendingIntent, and make it invoke when the user clicks. We'll also add a function to cancel the notification.

Let's create a project in Android and add two Button widgets to the activity_main.xml layout.The first button will be responsible for creating the notification, and the other for canceling it:

Now go to MainActivity, initialize the buttons and create methods for sending and canceling the notification:

As you can see, we didn't need any additional permissions in the manifest. Now just run the application.

As you can see in the picture above, we clicked on the "Create Notification" button, then it appeared in the notification bar with an Alert icon. Then we lowered the "notification curtain" and saw a linked Notification object. After clicking on the notification, we were redirected to the specified site.

 

You just have to cancel the notification: just click the "Cancel" button in the application and notificationManager will remove it by the specified id.