We’ve discussed and implemented basic Notifications in this post. In this tutorial, we’ll be looking into more advanced features of android notification and styling our notifications in different ways.
Android Notification Styling
Android Notification have plenty of powerful features that when used correctly would significantly improve the UX and user engagement. Some noteworthy features that we’ll be covering in this Android Notification tutorial are listed below.
- Notification Actions
- Heads Up Notification
- Big Text Style Notification
- Big Picture Style Notification
- Inbox Style Notification
- Message Style Notification
Android notification consists of three major components:
- Small Icon (often our app logo)
- Title
- Content Text
The introduction of Android Nougat (API 24) has given a new look to notifications as shown below.
The large icon now appears on the right. There’s an arrow besides the notification title that lets us expand, collapse the notification.
In this tutorial, we’ll be styling our notifications using some pre-defined awesome styles that Android has in store for us. We’ll be covering the following features at length.
- Android Notification Actions
- Heads Up Notifications
- Rich Notifications
Android Notification Actions
Android Notification actions are basically buttons placed below the body of the notification. A Notification action must include an icon, a label, and a PendingIntent
to be fired when the action is selected by the user.
With the Introduction of Android N, the icons are omitted from the action buttons to give space to other components.
An example of notification actions in Pre Nougat devices is given below.
An example of Notification Actions in Android N and above is given below.
Notification Action in Android Nougat
Heads Up Notifications
With the introduction of Android Lollipop (API level 21), notifications can appear in a small floating window (also called a heads-up notification) when the device is active (that is, the device is unlocked and its screen is on).
Such type of notifications are commonly seen when you’re using an application and you get a call. A small floating notification known as heads up notifications pops up with the notification actions to accept or decline a call.
Rich Notifications
Android allows us to add rich styles to our application with the introduction of Android L. Using these styles would make the notification look more appealing than ever. Some of the known styles that are used in many applications are listed below and are self-explanatory.
- BigTextStyle
- BigPictureStyle
- InboxStyle
- MessageStyle
We know that notifications on Android N can be expanded using the arrows. To expand notifications on pre-Nougat versions, you can swipe down over the notification using two fingers.
Not all Android versions would support the above styles. In case an Android OS doesn’t support the rich style, that style would simply be ignored.
Now let’s jump into the business end of this tutorial and develop an application that’ll have all the above features.
Android Notification Tutorial Project Structure
Android Notification Tutorial Code
The code for the activity_main.xml
is given below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:app="https://schemas.android.com/apk/res-auto" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.journaldev.stylingnotifications.MainActivity"> <Button android:id="@+id/btnNotificationActions" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="NOTIFICATION ACTIONS" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.196" /> <Button android:id="@+id/btnHeadsUp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginTop="8dp" android:text="HEADS UP NOTIFICATION" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/btnNotificationActions" /> <Button android:id="@+id/btnBigTextStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="BIG TEXT STYLE" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/btnHeadsUp" /> <Button android:id="@+id/btnBigPictureStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="BIG PICTURE STYLE" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/btnBigTextStyle" /> <Button android:id="@+id/btnInboxStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="INBOX STYLE" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/btnBigPictureStyle" /> <Button android:id="@+id/btnMessageStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="MESSAGE STYLE" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/btnInboxStyle" /> </android.support.constraint.ConstraintLayout> |
We’ve added a button for each type of notification that we’ll be discussing.
The skeleton code for the MainActivity.java
is given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
public class MainActivity extends AppCompatActivity implements View.OnClickListener { Button btnNotificationActions, btnHeadsUpNotification, btnBigTextStyle, btnBigPictureStyle, btnInboxStyle, btnMessageStyle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); clearNotification(); btnNotificationActions = (Button) findViewById(R.id.btnNotificationActions); btnHeadsUpNotification = (Button) findViewById(R.id.btnHeadsUp); btnBigTextStyle = (Button) findViewById(R.id.btnBigTextStyle); btnBigPictureStyle = (Button) findViewById(R.id.btnBigPictureStyle); btnInboxStyle = (Button) findViewById(R.id.btnInboxStyle); btnMessageStyle = (Button) findViewById(R.id.btnMessageStyle); btnNotificationActions.setOnClickListener(this); btnHeadsUpNotification.setOnClickListener(this); btnBigTextStyle.setOnClickListener(this); btnBigPictureStyle.setOnClickListener(this); btnInboxStyle.setOnClickListener(this); btnMessageStyle.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnNotificationActions: notificationActions(); break; case R.id.btnHeadsUp: headsUpNotification(); break; case R.id.btnBigTextStyle: bigTextStyleNotification(); break; case R.id.btnBigPictureStyle: bigPictureStyleNotification(); break; case R.id.btnInboxStyle: inboxStyleNotification(); break; case R.id.btnMessageStyle: messageStyleNotification(); break; } } public PendingIntent getLaunchIntent(int notificationId, Context context) { Intent intent = new Intent(context, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); intent.putExtra("notificationId", notificationId); return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); } private void clearNotification() { int notificationId = getIntent().getIntExtra("notificationId", 0); NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); manager.cancel(notificationId); } private void notificationActions() { //Logic goes here. } private void headsUpNotification() { //Logic goes here. } private void bigTextStyleNotification() { //Logic goes here. } private void bigPictureStyleNotification(); { //Logic goes here. } private void inboxStyleNotification() { //Logic goes here. } private void messageStyleNotification() { //Logic goes here. } } |
The method clearNotification()
is used to clear any existing notifications from the notification bar.
The method getLaunchIntent()
returns an instance of PendingIntent which when triggered from the notification, will relaunch the application.
Before we delve into the implementation of each type of notification, let’s define the BroadcastReceiver as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.journaldev.stylingnotifications; import android.app.NotificationManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class NotificationReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { int notificationId = intent.getIntExtra("notificationId", 0); NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); manager.cancel(notificationId); } } |
Update the AndroidManifest.xml
file with the receiver defined as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.journaldev.stylingnotifications"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <receiver android:name=".NotificationReceiver" android:exported="false"/> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
Adding actions inside a notification
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
private void notificationActions() { int NOTIFICATION_ID = 1; NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.jd); builder.setColor(ContextCompat.getColor(this, R.color.colorPrimaryDark)); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.jd)); builder.setContentTitle("Notification Actions"); builder.setContentText("Tap View to launch our website"); builder.setAutoCancel(true); PendingIntent launchIntent = getLaunchIntent(NOTIFICATION_ID, getBaseContext()); Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.journaldev.com")); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); Intent buttonIntent = new Intent(getBaseContext(), NotificationReceiver.class); buttonIntent.putExtra("notificationId", NOTIFICATION_ID); PendingIntent dismissIntent = PendingIntent.getBroadcast(getBaseContext(), 0, buttonIntent, 0); builder.setContentIntent(launchIntent); builder.addAction(android.R.drawable.ic_menu_view, "VIEW", pendingIntent); builder.addAction(android.R.drawable.ic_delete, "DISMISS", dismissIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // Will display the notification in the notification bar notificationManager.notify(NOTIFICATION_ID, builder.build()); } |
In the above code, we set the various styles on the instance builder
.
setColor()
sets the custom color for the notification icon, title and action button texts.
addAction()
is used to set the action buttons beneath the notification content. It expects three params: icon, text and the instance of PendingIntent.
setContentIntent()
sets the PendingIntent that’ll be triggered when the body of the notification is clicked. In the above code we’ve simply added the PendingIntent to relaunch the application.
setAutoCancel(true) is used to dismiss the notification when its clicked.
NotificationManager
class is used to display the notification.
The output of the application when the above type of notification is triggered is given below.
Note:
- When the VIEW button is clicked, the url is launched in the browser but the notification isn’t dismissed.
- When the DISMISS button is clicked the notification is cleared but the notification tray stays open.
- When the Notification content is clicked, the notification is dismissed as well as the activity is re-launched. This is where
getLaunchIntent()
andclearNotification()
methods are invoked.
Implementing Heads Up Notification
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
private void headsUpNotification() { int NOTIFICATION_ID = 1; NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.jd) .setContentTitle("Heads Up Notification") .setContentText("View the latest Swift Tutorial") .setAutoCancel(true) .setDefaults(NotificationCompat.DEFAULT_ALL) .setPriority(NotificationCompat.PRIORITY_HIGH); Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.journaldev.com/15126/swift-function")); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); Intent buttonIntent = new Intent(getBaseContext(), NotificationReceiver.class); buttonIntent.putExtra("notificationId", NOTIFICATION_ID); PendingIntent dismissIntent = PendingIntent.getBroadcast(getBaseContext(), 0, buttonIntent, 0); builder.addAction(android.R.drawable.ic_menu_view, "VIEW", pendingIntent); builder.addAction(android.R.drawable.ic_delete, "DISMISS", dismissIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notificationManager.notify(NOTIFICATION_ID, builder.build()); } |
To set a notification as heads up notification, two properties need to set on the builder instance.
1 2 3 |
setDefaults(NotificationCompat.DEFAULT_ALL) setPriority(NotificationCompat.PRIORITY_HIGH) |
Swiping a heads up notification would dismiss it. If it’s not dismissed, the heads-up notifications will fade away and change into a standard notification in the status bar.
The output of heads up notification is given below.
BigTextStyle Notification
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
private void bigTextStyleNotification() { int NOTIFICATION_ID = 1; PendingIntent launchIntent = getLaunchIntent(NOTIFICATION_ID, getBaseContext()); Intent buttonIntent = new Intent(getBaseContext(), NotificationReceiver.class); buttonIntent.putExtra("notificationId", NOTIFICATION_ID); PendingIntent dismissIntent = PendingIntent.getBroadcast(getBaseContext(), 0, buttonIntent, 0); NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.jd); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.jd)); builder.setContentTitle("Big Text Style"); builder.setStyle(new NotificationCompat.BigTextStyle().bigText(getResources().getString(R.string.lorem_ipsum))); builder.setAutoCancel(true); builder.setContentIntent(launchIntent); builder.addAction(android.R.drawable.ic_delete, "DISMISS", dismissIntent); builder.addAction(android.R.drawable.ic_menu_send, "OPEN APP", launchIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // Will display the notification in the notification bar notificationManager.notify(NOTIFICATION_ID, builder.build()); } |
A notification can be customised into a big text style notification by setting the style as
NotificationCompat.BigTextStyle()
. The string to be displayed is entered inside the method bigText().
The output of above type of notification is given below.
BigPictureStyle Notification
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
private void bigPictureStyleNotification() { int NOTIFICATION_ID = 1; Bitmap pic = BitmapFactory.decodeResource(getResources(), R.drawable.bg); Intent buttonIntent = new Intent(getBaseContext(), NotificationReceiver.class); buttonIntent.putExtra("notificationId", NOTIFICATION_ID); PendingIntent dismissIntent = PendingIntent.getBroadcast(getBaseContext(), 0, buttonIntent, 0); PendingIntent launchIntent = getLaunchIntent(NOTIFICATION_ID, getBaseContext()); NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.jd); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.jd)); builder.setContentTitle("Big Picture Style"); builder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(pic)); builder.setAutoCancel(true); builder.setContentIntent(launchIntent); builder.addAction(android.R.drawable.ic_delete, "DISMISS", dismissIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // Will display the notification in the notification bar notificationManager.notify(NOTIFICATION_ID, builder.build()); } |
For BigPicture to be displayed inside a notification the style is set as NotificationCompat.BigPictureStyle().bigPicture(bitmap))
.
The output with the above type of notification is given below.
InboxStyle Notification
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
private void inboxStyleNotification() { int NOTIFICATION_ID = 1; PendingIntent launchIntent = getLaunchIntent(NOTIFICATION_ID, getBaseContext()); NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.jd); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.jd)); builder.setStyle(new NotificationCompat.InboxStyle().addLine("Hello").addLine("Are you there?").addLine("How's your day?").setBigContentTitle("3 New Messages for you").setSummaryText("Inbox")); builder.setAutoCancel(true); builder.setContentIntent(launchIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // Will display the notification in the notification bar notificationManager.notify(NOTIFICATION_ID, builder.build()); } |
An inbox style notification is set by using the style new NotificationCompat.InboxStyle()
.
Each message is placed inside the method addLine()
. The summary text of all the messages is placed inside the method setSummaryText()
.
setContentTitle()
is replaced with setBigContentTitle()
in this style
The output of the above type of notification is given below
Message Style Notification
Message Style is introduced with Android N. Typically used for chats.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
private void messageStyleNotification() { int NOTIFICATION_ID = 1; PendingIntent launchIntent = getLaunchIntent(NOTIFICATION_ID, getBaseContext()); NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.jd); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.jd)); builder.setContentTitle("Messages"); builder.setStyle(new NotificationCompat.MessagingStyle("Teacher").setConversationTitle("Q&A Group") .addMessage("This type of notification was introduced in Android N. Right?",0,"Student 1") .addMessage("Yes",0,null) .addMessage("The constructor is passed with the name of the current user. Right?",0,"Student 2") .addMessage("True",0,null)); builder.setAutoCancel(true); builder.setContentIntent(launchIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // Will display the notification in the notification bar notificationManager.notify(NOTIFICATION_ID, builder.build()); } |
In the above code NotificationCompat.MessagingStyle(String)
contains a string that represents the current user(Typically in a chat its you!).
Each message is added in the method addMessage() with the timestamp as well as sender name.
If the sender name is set to null it signifies that the message is from the current user(you) and the name is taken from the constructor.
The output of the above type of notification is given below.
Adding all the above methods in the MainActivity.java
would give us the below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
package com.journaldev.stylingnotifications; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v4.app.NotificationCompat; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity implements View.OnClickListener { Button btnNotificationActions, btnHeadsUpNotification, btnBigTextStyle, btnBigPictureStyle, btnInboxStyle, btnMessageStyle; NotificationCompat.Builder builder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); clearNotification(); btnNotificationActions = (Button) findViewById(R.id.btnNotificationActions); btnHeadsUpNotification = (Button) findViewById(R.id.btnHeadsUp); btnBigTextStyle = (Button) findViewById(R.id.btnBigTextStyle); btnBigPictureStyle = (Button) findViewById(R.id.btnBigPictureStyle); btnInboxStyle = (Button) findViewById(R.id.btnInboxStyle); btnMessageStyle = (Button) findViewById(R.id.btnMessageStyle); btnNotificationActions.setOnClickListener(this); btnHeadsUpNotification.setOnClickListener(this); btnBigTextStyle.setOnClickListener(this); btnBigPictureStyle.setOnClickListener(this); btnInboxStyle.setOnClickListener(this); btnMessageStyle.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnNotificationActions: notificationActions(); break; case R.id.btnHeadsUp: headsUpNotification(); break; case R.id.btnBigTextStyle: bigTextStyleNotification(); break; case R.id.btnBigPictureStyle: bigPictureStyleNotification(); break; case R.id.btnInboxStyle: inboxStyleNotification(); break; case R.id.btnMessageStyle: messageStyleNotification(); break; } } private void notificationActions() { int NOTIFICATION_ID = 1; builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.jd); builder.setColor(ContextCompat.getColor(this, R.color.colorPrimaryDark)); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.jd)); builder.setContentTitle("Notification Actions"); builder.setContentText("Tap View to launch our website"); builder.setAutoCancel(true); PendingIntent launchIntent = getLaunchIntent(NOTIFICATION_ID, getBaseContext()); Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.journaldev.com")); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); Intent buttonIntent = new Intent(getBaseContext(), NotificationReceiver.class); buttonIntent.putExtra("notificationId", NOTIFICATION_ID); PendingIntent dismissIntent = PendingIntent.getBroadcast(getBaseContext(), 0, buttonIntent, 0); builder.setContentIntent(launchIntent); builder.addAction(android.R.drawable.ic_menu_view, "VIEW", pendingIntent); builder.addAction(android.R.drawable.ic_delete, "DISMISS", dismissIntent); buildNotification(NOTIFICATION_ID); } public PendingIntent getLaunchIntent(int notificationId, Context context) { Intent intent = new Intent(context, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); intent.putExtra("notificationId", notificationId); return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); } private void clearNotification() { int notificationId = getIntent().getIntExtra("notificationId", 0); NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); manager.cancel(notificationId); } private void headsUpNotification() { int NOTIFICATION_ID = 1; builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.jd) .setContentTitle("Heads Up Notification") .setContentText("View the latest Swift Tutorial") .setAutoCancel(true) .setDefaults(NotificationCompat.DEFAULT_ALL) .setPriority(NotificationCompat.PRIORITY_HIGH); Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.journaldev.com/15126/swift-function")); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); Intent buttonIntent = new Intent(getBaseContext(), NotificationReceiver.class); buttonIntent.putExtra("notificationId", NOTIFICATION_ID); PendingIntent dismissIntent = PendingIntent.getBroadcast(getBaseContext(), 0, buttonIntent, 0); builder.addAction(android.R.drawable.ic_menu_view, "VIEW", pendingIntent); builder.addAction(android.R.drawable.ic_delete, "DISMISS", dismissIntent); buildNotification(NOTIFICATION_ID); } private void bigTextStyleNotification() { int NOTIFICATION_ID = 1; PendingIntent launchIntent = getLaunchIntent(NOTIFICATION_ID, getBaseContext()); Intent buttonIntent = new Intent(getBaseContext(), NotificationReceiver.class); buttonIntent.putExtra("notificationId", NOTIFICATION_ID); PendingIntent dismissIntent = PendingIntent.getBroadcast(getBaseContext(), 0, buttonIntent, 0); builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.jd); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.jd)); builder.setContentTitle("Big Text Style"); builder.setStyle(new NotificationCompat.BigTextStyle().bigText(getResources().getString(R.string.lorem_ipsum))); builder.setAutoCancel(true); builder.setContentIntent(launchIntent); builder.addAction(android.R.drawable.ic_delete, "DISMISS", dismissIntent); builder.addAction(android.R.drawable.ic_menu_send, "OPEN APP", launchIntent); buildNotification(NOTIFICATION_ID); } private void bigPictureStyleNotification() { int NOTIFICATION_ID = 1; Bitmap pic = BitmapFactory.decodeResource(getResources(), R.drawable.bg); Intent buttonIntent = new Intent(getBaseContext(), NotificationReceiver.class); buttonIntent.putExtra("notificationId", NOTIFICATION_ID); PendingIntent dismissIntent = PendingIntent.getBroadcast(getBaseContext(), 0, buttonIntent, 0); PendingIntent launchIntent = getLaunchIntent(NOTIFICATION_ID, getBaseContext()); builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.jd); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.jd)); builder.setContentTitle("Big Picture Style"); builder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(pic)); builder.setAutoCancel(true); builder.setContentIntent(launchIntent); builder.addAction(android.R.drawable.ic_delete, "DISMISS", dismissIntent); buildNotification(NOTIFICATION_ID); } private void inboxStyleNotification() { int NOTIFICATION_ID = 1; PendingIntent launchIntent = getLaunchIntent(NOTIFICATION_ID, getBaseContext()); builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.jd); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.jd)); builder.setContentTitle("Messages"); builder.setStyle(new NotificationCompat.InboxStyle().addLine("Hello").addLine("Are you there?").addLine("How's your day?").setBigContentTitle("3 New Messages for you").setSummaryText("Inbox")); builder.setAutoCancel(true); builder.setContentIntent(launchIntent); buildNotification(NOTIFICATION_ID); } private void messageStyleNotification() { int NOTIFICATION_ID = 1; PendingIntent launchIntent = getLaunchIntent(NOTIFICATION_ID, getBaseContext()); builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.jd); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.jd)); builder.setContentTitle("Messages"); builder.setStyle(new NotificationCompat.MessagingStyle("Teacher").setConversationTitle("Q&A Group") .addMessage("This type of notification was introduced in Android N. Right?", 0, "Student 1") .addMessage("Yes", 0, null) .addMessage("The constructor is passed with the name of the current user. Right?", 0, "Student 2") .addMessage("True", 0, null)); builder.setAutoCancel(true); builder.setContentIntent(launchIntent); buildNotification(NOTIFICATION_ID); } private void buildNotification(int NOTIFICATION_ID) { NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // Will display the notification in the notification bar notificationManager.notify(NOTIFICATION_ID, builder.build()); } } |
This brings an end to android notification tutorial. We’ve styled our notifications in some interesting ways. You can download the final Android notification example project from the link below.
Reference: Official Doc