Creating Pending Intent in Android — A step by step guide

Archit Gupta
6 min readMay 13, 2018

--

Here I am gonna show you how to build a Pending Intent taking you from the beginning that is definition of Pending Intent to all over a working example. So lets start :)

INTRODUCTION

Notificatons are a way to notify the user of background updates as there is no use of an background update if it doesn’t even get to be known, right ?

These have become a lighweight method to interact directly with the apps. From lollipop, notifications started including few actions in them for limited data available in them, such as ans or reject a call from notification itself when it arrives. From nougat, notifications started to show the name of the app which is firing them

We are continuing here with the same example I took in my previous article (please refer it) and we are gonna notify the user every now and then to drink water.

Also we want our notification to launch the app when clicked so that we can tap the image to update water count when drank. For notification to launch our app we need to use Pending Intent. Normal intents are used to launch other activities internally or externally and for that, we always need to add certain permissions in manifest. Now we are trying to do opposite i.e. launch the activity in our application from other application, notification here

Any notification in system is displayed using System Service called NotificationManager. A system service is a service which is started by the android system itself and is not part of our application processes at all.

Now if we want Notification Manager to launch an activity in my application, I need permissions to do so but I cant modify system’s service’s permission. (for which pending intent comes into play)

PENDING INTENT

A pending intent is a wrapper around regular intent that is designed to be used by another application.

It gives that other application the ability to perform the included action as it was your application with all the permissions your application has been granted

It can launch Services, Private activities, Broadcast protected intents even when my application is not running. Calling our activity, service or broadcast activity from Pending intent requires four parameters in the method

→ requestCode — an integer which is assigned to the pending intent we are working with to refer to it when we want to delete it

→ flags — used to handle multiple intents using same Pending intent instance

Use FLAG_CANCEL_CURRENT or FLAG_UPDATE_CURRENT in case of single intent

This is a sample code for creating Pending Intent using getService method. This method call returns an actual instance of the pending intent that you will pass to another app which in our case is Notification Manager which will further launch our activity

CODE

Right now, we will create a button (TEST NOTIFICATION) in our activity clicking on which will generate our notification for test purposes for now.

All our notification code will go in NotificationUtils class

→ Creating helper methods to help us build and display the notifications — contentIntent() & largeIcon()

→ contenIntent method creates and return a Pending Intent that we will pass to the notification to allow us to launch the desired activity when clicked on notification

Inside the method we create a simple intent to launch our activity, we will pass this intent to Pending Intent

Then we call getActivity method for Pending Intent which will wrap this explicit intent we defined above to convert it to Pending Intent

getActivity takes 4 parameters out of which the second parameter is an ID which is used to make this Pending Intent unique, 4th parameter is kept to FLAG_UPDATE_CURRENT. When a duplicate Pending Intent is created, this tag will be responsible for keeping the previous instance intact and just update it with the new content in incoming Pending Intent.

→ Second helper method return a Bitmap which is to be displayed in the notification

To convert an image kept in resources folder into a bitmap, we use the BitmapFactory method and pass in the res variable which refers to the resources folder

Now lets create the actual Notification

→ We build our notification in remindUserBecauseCharging method

→ This method will be responsible for creating the notification, the notification channel to which it belongs and also be responsible for displaying that notification

→ Get a reference to notification manager which is system service that why we have used getSystemService method

→ After Oreo, it is not allowed to create a notification without having a notification channel, that’s y we create a channel in case the version is above or equal to oreo.

It is done by calling NotificationChannel constructor which takes a string ID, a name for the channel and the importance level which is kept at high to force the notification to pop up on device using heads up display.

It is surrounded by an if condition to check for the Oreo version

→ Now we build the notification using Notification Builder whose constructor takes the context and the ID of the channel in which it belongs

We add here many attributes to define how the notification would look like such as the icons and title, text to be displayed

We add the default to vibration (Remember to add the permission in manifest)

Finally we add the intent we declared in our helping method

AutoCancel gaurantees that notification will go away when clicked

→ Previously we did set the channel importance high but what about versions less than oreo ??

We check for the versions greater than jelly bean and less than oreo and set the builder’s priority to high

→ Finally we call the notify method which will launch the notification which takes the ID which can be used to cancel the notification in future if we want to.

Finally, just create a dummy button in your Main Activity and call remindUserBecauseCharging() method we just created in the onClick of this button

Now one more thing needs to be done for the sake of performance

Suppose of the activity is already running and we click the notification. We don’t the notification to relaunch the activity in such case, therefor we change some settings in manifest, lets see

→ We just need to add the launchMode attribute in activity tag and set it to singleTop. This will ensure that if the activity is already running, it will bring it to foreground when notification is clicked instead of creating a new instance.

Pheww!! Everything is done… Now its your turn to test it and notify yourself that you have learned how to notify :D Do clap if you like guys… Keep supporting :)

--

--