Creating a simple android notification with example
|Android notification offers a short timely information about the app to the users. A simple android notification is made up of title, description and an icon. We can add interaction to android notification when user taps on it we launch an activity.
The video below illustrate How to show android notification which has tap action listener.
By default, android notification are set to fit on one line, if you want your notification to be longer you must setStyle using setStyle() method.
1 |
.setStyle(new NotificationCompat.BigTextStyle().bigText("A am a simple notification for learning purposes, and am going to show for a bigger text")) |
In this tutorial we are going to create a simple android notification with on tap listener.
To get started first we will add library dependency.
Contents
Adding support library
Android notification requires support library. Open your module-level
1 2 3 |
build.gradle |
Include the following support library if it doesn’t exist.
1 2 3 |
implementation "com.android.support:support-compat:28.0.0" |
Creating android notification
We are going to create a simple android notification that has a title, text and an icon.
1 2 3 4 5 6 7 8 9 |
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.ic_notifications_none_black_24dp) .setContentTitle("Simple notification") .setContentText("I am a simple notification for learning purposes") .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setAutoCancel(true) |
Other component we have added is
1 2 3 |
setPriority() and setAutoCancel(true) |
setPriority() this determines how intrusive notification should be and setAutoCancel(true) which automatically when the user taps it.
Displaying android notification
Once you have created you notification, now it’s time to make it appear call
1 |
NotificationManagerCompact.notify() |
and pass UniqueId and result of
1 |
NotificationCompact.Buider.Build() |
Save the unique id for later update or cancellation of a this notification.
1 2 |
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this); notificationManagerCompat.notify(NOTIFICATION_ID,builder.build()) |
Our final code for android notification.
1 2 3 4 5 6 7 8 9 10 11 12 |
public void createNotification(){ NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.ic_notifications_none_black_24dp) .setContentTitle("Simple notification") .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setAutoCancel(true) .setStyle(new NotificationCompat.BigTextStyle().bigText("A am a simple notification for learning purposes, and am going to show for a bigger text")) .setContentIntent(pendingIntent); NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this); notificationManagerCompat.notify(NOTIFICATION_ID,builder.build()); } |
To display your android notification, call the above method.
Android notification on Tap Action
Android notifications should respond to tap, Let’s tap action to our notification when a user taps on the notification a new activity will be launched.
Adding on tap listener to our notification, You will need to specify Notification Content Intent via Pending Intent Object and pass it to setContentIntent()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public void createNotification(){ Intent intent = new Intent(this,NotificationActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK); PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0); NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.ic_notifications_none_black_24dp) .setContentTitle("Simple notification") .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setAutoCancel(true) .setStyle(new NotificationCompat.BigTextStyle().bigText("A am a simple notification for learning purposes, and am going to show for a bigger text")) .setContentIntent(pendingIntent); NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this); notificationManagerCompat.notify(NOTIFICATION_ID,builder.build()); } |
Call the method above to display android notification which has on tap listener.
Note 1: NotificationActivity is the activity that is going to show when notification is tapped.
Note 2: Starting from android 8 and higher you need to register your app’s notification channel with the system before posting your notification.
This is done by passing an instance of notificationChannel to createNotificationChannel().
create notification channel for android 8 and above
1 2 3 4 5 6 7 8 9 10 |
public void createNotificationChannel(){ if (android.os.Build.VERSION.SDK_INT > = android.os.Build.VERSION_CODES.O) { NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID,CHANNEL_NAME,NOTIFICIATION_IMPORTANCE); notificationChannel.setDescription("This is a notification channel"); NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(notificationChannel); } } |
If you are dealing with android 8 and higher call this method when app starts. No had including it in android version 8 and below.
For additional help or clarification, kindly comment below.
To access the project source, use the github link below.
https://github.com/larntech/android_notification.git