Android Notification Example with Progress bar, Big Picture and Direct Reply
Hello Guys. In Android Notification Example tutorial, we will learn different different types of Android Notification Example like Progress Bar, Big Picture and Direct Reply in notification. At the end of tutorial, you will be able to make following three types of Android Notification Example:
- 1 Demo of Android Notification Example
- 2 What is Notification in Android
- 3 How to make Progress Bar in Android Notification
- 4 How to make Big Picture Style in Android Notification
- 5 How to make Direct Reply in Android Notification
- 6 Creating a New Project – Android Notifications Example
- 7 Support for Progress Bar in Android Notification
- 8 Support for Big Picture Notification Android
- 9 Enhanced Notifications in Android N with Direct Reply
- 10 What’s Next ??
Demo of Android Notification Example
Download full code here:
What is Notification in Android
An android notification is a message that a developer can display to the user outside of application’s normal UI. When you tell the system to issue a notification, it first appears as an icon in the notification area. On clicking notification, a new Activity is opened displaying details of android notification.
How to make Progress Bar in Android Notification
This notification generally appears whenever we download a particular image, song, video or any other file from internet. It display progress of download with a progress bar that appears as notification in Android.
How to make Big Picture Style in Android Notification
Bigpicturestyle notification appears in different News and E-Commerce websites. Here large-format notifications that include a large image attachment is appeared as notification in android.
How to make Direct Reply in Android Notification
This is new feature included in Notifications in Android N (Android Version 7.0). It is new MessagingStyle in android where user can directly write in notification and reply to other user.
Let’s start making these types of Android Notification Example. We hope you will enjoy it.:)
Creating a New Project – Android Notifications Example
- Open Android Studio and create a new project Android Speech to Text Tutorial and company domain application.example.com (We have used our own company domain i.e androidtutorialpoint.com. Similarly you can use yours).
- Click Next and choose Min SDK (It should be maximum possible). Again Click Next and Choose Blank Activity .
- Choose the Activity as MainActivity and click next.
- Leave all other things as default and Click Finish.
A new project will be created and gradle will resolve all the dependencies. Make Sure you build.gradle has following dependency to support latest Android Version.
Now let’s make Layout of our Android Notification Example. Make following changes in activity_main.xml:
In the above code we have created three buttons each for showing notifications of Progressive Bar, Big Picture and Direct Reply.
Also make another layout file named activity_notification.xml at same path (…/AndroidNotificationsExample/app/src/main/res/layout/activity_notification.xml). This file will be utilized for showing second activity which will be called when someone clicks on any notification.
In this tutorial, we will use two activities, First is ofcourse MainActivity and second is ResultActivity. ResultActivity is called whenever a user clicks on notification. We have to register this second Activity, so make following changes in AndroidManifest.xml:
In the above code, support for Android 4.0.3 and earlier is added by specifying the parent of the Activity by adding a element as the child of the .
For this element, set android:name=”android.support.PARENT_ACTIVITY”. Set android:value=”“.
Also support for Android 4.1 and later, android:parentActivityName attribute is added to theelement of the ResultActivity.
Now we will include main functionality in our code. We will divide it into three parts according to each of three Android Notification Example. We will setOnClickListener for each of the button in MainActivity.
Support for Progress Bar in Android Notification
Add following code in onCreate() method of MainActivity.
Code of Progress Bar in Android Notification
In the above code, First of all we have created an Intent to start Activity. Then stack builder is created by calling TaskStackBuilder.create(). TaskStackBuilder is used to go back to the previous activity after clicking on notification. For Example: When you click on a notification then a new Activity appears (In our case ResultActivity). Now if you want to go back to the previous Activity (In our case MainActivity) then Stack Builder will be used.
In an android Notification, action is defined by a PendingIntent containing an Intent that starts an Activity in application. To associate the PendingIntent with a gesture, we have called NotificationCompat.Builder. Also to start an Activity when the user clicks the notification text in the notification drawer, we have added the PendingIntent by calling setContentIntent().
Android Notification is created through NotificationCompat.Builder.build(), which returns a Notification object containing our specifications. In our case returned object is mBuilder. To issue the notification, we passed the Notification object to the system by calling NotificationManager.notify().
The major point here is mBuilder.setProgress(0,0,true). To display an indeterminate activity indicator, we have added it in our notification with setProgress(0, 0, true) (the first two arguments are ignored). So here a progress bar will be shown in notification just like shown in an actual download. Progress Bar Notification will look as follows:
So our code for Progress bar in Android notification is complete. Now let’s develop second android notification example i.e. BigPictureStyle. This will be an android notification with image example.
Support for Big Picture Notification Android
In this android notification example, we will create a notification just like you see in Amazon or some news app. There will be a large image shown in notification. This will be created on clicking Button1.
Code of Big image in Notification Android
Above code is pretty much self explanatory in line with progress bar code. There is one crucial difference as compared to ProgressBar and that is addition of BigPictureStyle. Notification.BigPictureStyle is a helper class for generating large-format notifications that include a large image attachment. It only accepts images in the form of bitmap so we casted drawable resource into Bitmap. Following image shows how it will look like:
Now comes the final example of Android Notification which is direct reply.
Enhanced Notifications in Android N with Direct Reply
Starting in Android 7.0 (API level 24), users can respond directly to text messages or update task lists from within the notification dialog. On a handheld, the inline reply action appears as an additional button displayed in the notification.
Note: You can only use this feature in Android 7.0. So either run this particular example in a smartphone supporting Android 7.0 or run in an emulator.
Direct Reply notification will appear if you click on Button3. Add following code in MainActivity:
Code of Direct Reply in Android N Notification
In the above code, we have created an instance of RemoteInput.Builder. It adds a RemoteInput to a Notification.Action, sets the result key as quick_reply, and sets the label as Quick reply. Users are prompted to input a response when they trigger the action. The results are sent along with the intent and can be retrieved with the result key (provided to the RemoteInput.Builder constructor) from the Bundle returned by getResultsFromIntent(Intent).
Then we have attached the RemoteInput object to an action using addRemoteInput(). Finally this action is added to the notification through .addAction(action) in NotificationCompat.Builder. This is how direct reply is enabled in notification.
So our code MainActivity.java is complete. You can see full code of MainActivity here:
Now in the last step, we will create a second Activity named ResultActivity which will be called whenever a user clicks on notification. Also whenever a user write some reply in notification and click on “Reply” then it will appear as text in this Activity.
Create a new file named ResultActivity.java at the same path where MainActivity.java is located. Add following code:
In the above code, we have called getResultsFromIntent() by passing the notification action’s intent as the input parameter. This method returns a Bundle that contains the text response. Then required text is written using textView.setText(getMessageText(getIntent()));. getMessageText will grab input from notification and return it. Finally notification is build and issued using same notification ID that we provided for the previous notification i.e mRequestCode.
So our code of Android Notification Example is complete. Run this code. Following screen will appear:
Now when you click on each button, different notifications will appear. For Example If you click on “Show Reply Notification” then following will appear where you can write any text:
On clicking Reply following screen will appear showing the message which you write in notification:
You can see its demo in the YouTube video given at start of tutorial.
What’s Next ??
We hope you liked our Android Notification Example, please leave comments in the comment section below in the case of any doubts. Let us know how do you liked this Android Notification Example.
148 total views, 4 views today