Latest

Learn how to make professional notifications on Android

Here's a collection of Android programming tutorials on how to construct all types of notifications on Android and explore methods to implement totally different types and features and options that we introduced for notifi cation.

This part consists of the next subjects:

  • Understanding the Notification Framework on Android OS
  • Introduce the PendingIntent technique to interact with the notifications and direct it to the vacation spot
  • Work with the Action buttons on the notification
  • Difference in the best way the motion is displayed in the previous and new versions of Android
  • Show lengthy text within the notification by BigTextStyle fashion and embrace textual content summary in the notification.
  • Introducing Announcements that Expandable Notification
  • View giant image in Notification utilizing BigPictureStyle type
  • How to convert photographs in JPG and PNG format to Bitmap on Android
  • Show record of acquired messages by InboxStyle
  • Eradicating or canceling notifications after a selected action or routinely after a specified time
  • Notification Badge management, resembling the best way notifications are displayed, as well as the variety of acquired messages
  • How to disable Notification Badge or Notification Dots
  • Deleting a Notification Channel from the Android working system by deleteNotificationChannel

This matter is offered in pdf format and on 27 pages. You will notice the following several primary pages:

In the previous section, we received to know how to build notifications on Android. On this dialogue, I intend to introduce several types of notification, modes and numerous prospects for implementation of Android notifications.
I can’t create a brand new venture for this half and will use the identical challenge as the earlier one.

Android's Notification Framework

In the identify of Allah. Before continuing with the topic first, it's better to evaluate the neti fi cation structure we created within the earlier discussion. I’ll clarify each of the elements within the order quantity:

1: Small icon: This icon is made by setSmallIcon whose definition is obligatory for notifications.
2: Here is the identify of the appliance that runs the notification.
three: Indicates the time of creating the notification. You’ll be able to, in fact, change or delete this part as you would like.
four: Giant icon: This icon is created by setLargeIcon and its software displays a big icon or giant picture subsequent to the message.
5: Notification Title
6: Notification textual content

Notifications on Android

Notifications could be carried out in several methods, each of which has its own software. A notification solely informs the consumer a few temporary message. Another provides the consumer extra details and directs him to an activity within the app or an internet page. And there are different kinds of how to build and implement notifications, which we’ll look at here.

Notice: The complete code of activity, as well as to the supply of the undertaking, can also be included at the finish of this tutorial. So, at each step, in case you are confused, refer to the complete code. To summarize the text, I duplicated the duplicate description (including making the button, defining it in the lively class, developing the setOnClickListener technique, and so on.) as far as potential.

Word: The implementation of the principles and design rules of material in the notifications is essential. Assets like this hyperlink assist us to better understand the design particulars.

Use PendingIntent and Build Motion for Notification

The notation we've already constructed will solely display a message to the consumer and, aside from closing it, the consumer cannot work together with this message. If we’d like this interaction. If the announcement is a reduction, it should direct the consumer to the linked page. Here’s a pendingIntent. Previously, we acquired to know how to work with intent on Android. Now, within the notification we use the intent to decide what occurs after the consumer touches the notification.
Open the Notification undertaking. First, I add a button referred to as PendingIntent Notification to the Exercise Format:

Making a Notification on Android by PendingIntent

Then, in the code under, I make another notation in activity:

package deal and.android_studio.notification;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content material.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Shade;
import android.media.RingtoneManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity

    Button simpleButton, pendingButton;
    NotificationManager notifManager;
    String offerChannelId = “Offers”;
    PendingIntent mpIntent;

    @Override
    protected void onCreate (Bundle savedInstanceState)
        tremendous.onCreate (savedInstanceState);
        setContentView (R.format.activity_main);

        simpleButton = findViewById (R.id.simple_notif);
        pendingButton = findViewById (R.id.pending_notif);

        notifManager = (NotificationManager) getSystemService (Context.NOTIFICATION_SERVICE);

        Intent pIntent = new Intent (this, CartActivity.class);
        mpIntent = PendingIntent.getActivity (this, 0, pIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        simpleButton.setOnClickListener (new View.OnClickListener ()
            @Override
            public void onClick (View view)

                createNotifChannel ();
                simpleNotification ();

            
        );

        pendingButton.setOnClickListener (new View.OnClickListener ()
            @Override
            public void onClick (View view)

                createNotifChannel ();
                pendingNotification ();

            
        );

    

    personal void createNotifChannel ()

        if (Construct.VERSION.SDK_INT> = Construct.VERSION_CODES.O)

            String offerChannelName = “Shop offers”;
            String offerChannelDescription = “Best offers for customers”;
            int offerChannelImportance = NotificationManager.IMPORTANCE_DEFAULT;

            NotificationChannel notifChannel = new NotificationChannel (offerChannelId, offerChannelName, offerChannelImportance);
            notifChannel.setDescription (offerChannelDescription);
            //notifChannel.enableVibration(true);
            notifChannel.enableLights (true);
            notifChannel.setLightColor (Shade.GREEN);

            notifManager.createNotificationChannel (notifChannel);

        

    

    public void simpleNotification ()

        NotificationCompat.Builder sNotifBuilder = new NotificationCompat.Builder (this, offerChannelId)
                .setSmallIcon (R.drawable.notif_icon)
                .setContentTitle (“Upgrade”)
                .setContentText (“A new version of the program is ready to be downloaded”)
                .setVibrate (new lengthy[]100,500,500,500,500,500)
                .setLargeIcon (BitmapFactory.decodeResource (getResources (), R.drawable.android))
                .setSound (RingtoneManager.getDefaultUri (RingtoneManager.TYPE_NOTIFICATION))
                .setVisibility (NotificationCompat.VISIBILITY_PUBLIC);

        notifManager.notify (1, sNotifBuilder.build ());

    

    public void pendingNotification ()

        NotificationCompat.Builder pNotifBuilder = new NotificationCompat.Builder (this, offerChannelId)
                .setSmallIcon (R.drawable.notif_icon)
                .setContentTitle (“Fifty percent discount!”)
                .setContentText (“50% discount on sunrise”)
                .setLargeIcon (BitmapFactory.decodeResource (getResources (), R.drawable.android))
                .setContentIntent (mpIntent);

        notifManager.notify (2, pNotifBuilder.build ());

    

Within the code above, I first related the pendingButton to the corresponding button in Leah. Then I created an Intent object referred to as mIntent:

Intent pIntent = new Intent (this, CartActivity.class);

Due to the tangibility of the end result, I added an activity referred to as CartActivity to the challenge and introduced it to the intent.

mpIntent = PendingIntent.getActivity (this, 0, pIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Then, using the PendingIntent technique and getActivity, I arrange pIntent by touching the notification. This technique takes 4 parameters as proven in the table under:

PendingIntent input parameters

The first one is the context that I entered. The second parameter is an int worth that’s thought-about a personal request code. I put 0 for this notification. The third parameter is the intent that I already outlined, i.e., pIntent. The last parameter is flags to decide the standing of the intent, which I chosen here as FLAG_UPDATE_CURRENT.
Subsequent, I added a perform referred to as pendingNotification to the category. The first technique of this perform is analogous to simpleNotification, which is introduced in the previous dialogue.

public void pendingNotification ()

    NotificationCompat.Builder pNotifBuilder = new NotificationCompat.Builder (this, offerChannelId)
            .setSmallIcon (R.drawable.notif_icon)
            .setContentTitle (“Fifty percent discount!”)
            .setContentText (“50% discount on sunrise”)
            .setLargeIcon (BitmapFactory.decodeResource (getResources (), R.drawable.android))
            .setContentIntent (mpIntent);

    notifManager.notify (2, pNotifBuilder.construct ());

I introduce the setIntentIntent I've already made to the notification.
Lastly, we create the setOnClickListener technique for the pendingButton button and call the features related to the channel and the notification. (For different notifications, the identical steps are repeated; that is, adding the button in the Format, developing the setOnClickListener technique, and calling the channel features and the corresponding notation inside that technique).
Run the challenge and click on the PendingIntent Notification button:

Performing Notification on Android

Delete notifications after clicking on it

By clicking on the notification, I was redirected to the CartActivity exercise. But there’s an objection. Take a look at the status bar of the second picture. Notification icon nonetheless visible. Meaning, although the consumer has clicked on the notification and moved to the meant vacation spot, the notification nonetheless remains to be resolved. That is carried out by the setAutoCancel technique:

NotificationCompat.Builder pNotifBuilder = new NotificationCompat.Builder (this, offerChannelId)
        .setSmallIcon (R.drawable.notif_icon)
        .setContentTitle (“Fifty percent discount!”)
        .setContentText (“50% discount on sunrise”)
        .setLargeIcon (BitmapFactory.decodeResource (getResources (), R.drawable.android))
        .setContentIntent (mpIntent)
        .setAutoCancel (true);

By re-running the undertaking, after clicking on the Notification, as well as to transferring the consumer to the CartActivity page, notification may even be deleted.
As well as to clicking on the notification, you possibly can add buttons to the notification, each of which has a distinct intent. These buttons are referred to as Motion.
For instance, SMS messaging notification usually has two options. The primary one is Answer to reply the acquired SMS, and the second Mark as learn, which touches this button, the SMS modifications from “unread” to “Read”.
I deactivate the setContentIntent technique, and as an alternative add two actions to the notification:

NotificationCompat.Builder pNotifBuilder = new NotificationCompat.Builder (this, offerChannelId)
        .setSmallIcon (R.drawable.notif_icon)
        .setContentTitle (“Fifty percent discount!”)
        .setContentText (“50% discount on sunrise”)
        .setLargeIcon (BitmapFactory.decodeResource (getResources (), R.drawable.android))
        //.setContentIntent(mpIntent)
        .addAction (R.drawable.notif_icon, “Buy”, mpIntent)
        .addAction (R.drawable.notif_icon, “Product details”, mpIntent)
        .setAutoCancel (true);

addAction takes three entries:
First Entry: An icon might be outlined for every choice. In fact, the observe from Android Nougat later, for the simplicity of the notification surroundings, these icons are usually not displayed.
Second Entry: The action title I entered for the primary motion “Buy” and the second “Product Details” action.
Third entry: PendingIntent as beforehand defined.

I run the challenge again:

Add the Action button to the Android notification via the addAction method

Two actions have been added to the top of the notification, although in this example, each have a perform; that’s, they’re directed to CartActivity.
As I discussed earlier, the choices icon shouldn’t be displayed from Android N and above. However on units under this version, there are icons beside the button title. I run the challenge on a tool with API 23:

Hide the notifications action icon on Android below N

Notice: Clicking on the actions takes the consumer to the vacation spot, however notifications are nonetheless not deleted. Because of this setAutoCancel doesn’t work right here. To unravel this drawback, the cancel technique is used on page 18.

Present Massive Text in Notification by BigTextStyle

Typically we’d like to display a longer textual content within the notification to the consumer. If we use setContentText for an extended text, just a few of its first phrases shall be seen. That is the place the types permit us to personalize in notifications. Take a look at the next code:

public void bigTextNotification ()

    NotificationCompat.BigTextStyle bStyle = new NotificationCompat.BigTextStyle ()
            .bigText (“Lorem Ipsum is a dummy text to design and print.” The printing business was whenever you needed to contemplate the registration and design thought to be exiting) ”
            .setBigContentTitle (“Long Notification”)
            .setSummaryText (“Long text abstract”);

    NotificationCompat.Builder bNotifBuilder = new NotificationCompat.Builder (this, offerChannelId)
            .setSmallIcon (R.drawable.notif_icon)
            .setContentTitle (“Fifty percent discount!”)
            .setContentText (“50% discount on sunrise”)
            .setStyle (bStyle);

    notifManager.notify (three, bNotifBuilder.build ());

Like before, I added a brand new perform to the category. I gave the identify of this perform bigTextNotification.
NotificationCompat permits you to outline the fashion. Right here I’ve created an object referred to as bStyle from BigTextStyle with 3 attributes outlined for it.

Download the PDF file to learn the tutorial

Notice: The supply of the undertaking is inside the Workouts folder

Provided that low-end primary training is offered to the consumer and the proceeds are solely used to fund the website and provide future training, sharing this file with others is towards ethics.

When you’ve got hassle downloading by way of the box under (download basket), by going to this hyperlink, select AdvancedNotification for 2000 Toman.

Download the PDF version of this tutorial together with the source of the challenge
Variety of pages: 26
Measurement: 2 MB
Worth: $ 2,000