Android Jelly Bean notifications with actions

Shashika's Web Blog

Recently I had to work with Android JB style notifications. Unlike previous versions, we can add custom actions to these new notifications. What I had to do was upload some files to a server using a foreground service and add an option to cancel the uploading. So I added the Cancel Upload action to my notification.

Image

To add this action I used addAction (int icon, CharSequence title, PendingIntent intent) method.  When the notification is tapped I needed to open the app but I didn’t want to open the app when the Cancel action is tapped.

After some research I was managed to achieve this using a broadcast receiver.

This is my foreground service with the notification.

You need to register the CancelUploadReceiver in the manifest file.

And when the “Cancel Upload” is tapped it will receive the broadcast. Then we can simply stop the service.

View original post

Leave a comment