Android APP_ Control - Notification notification

Posted by stereofrog on Fri, 18 Feb 2022 20:40:58 +0100

From: Android APP_ Control (6) -- Notification notification
Author: and pursung
Release time: 2021-04-02 00:30:14

Effect list

Implementation effect: click send notification, and the system sends a notification; You can jump to view through the drop-down taskbar; You can also directly click the message to jump to view; You can also click the button to cancel the notification. As shown below:

1, Create a notification manager

NotificationManager class is a notification manager class. This object is a service maintained by the system and is obtained in singleton mode. Therefore, this object is generally not instantiated directly.

In the Activity, you can use the getSystemService method to obtain the NotificationManager object, which can return the corresponding object through the handle of the Android system level service. You need to return the notification manager here, so you can pass the notification directly_ Service is enough.


NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

  • 1

NotificationManager is used for format conversion

2, Use the Builder constructor to create a Notification object

Using the Builder constructor of NotificationCompat class to create Notification objects can ensure that the program works normally on all versions. Android8.0 adds the concept of Notification channel. If it is not set, the Notification cannot be in Android 8.0 0 is displayed on the machine.

Notification notification = new NotificationCompat.Builder(this,"zhua")

  • 1

context: indicates the environment

3, Notification channel: NotificationCannel

Android8.0 introduces notification channels, which allow you to create user customizable channels for each type of notification you want to display.

Three important parameters:

(1) id: channelld, which is the channel id
(2) name: Information
(3) Importance: importance of notification

Notification importance is set in the NotificationManager class:

parameternoticeejectPrompt tonestatus bar

4, Set the properties of notification through chain structure

That is to set the notification properties for.

The specific attributes are described in detail in the following example code. Two error prone points are recorded below.

(1) setSmallIcon should pay attention to:

From Android 5 At the beginning of the 0 system, the design of the notification bar icon has been modified. Now Google requires that the notification bar icon of all applications should only be drawn with alpha layer, not RGB layer.

Learn to look at the parameters. For example, the following one requires you to pass in the rbg parameter to set the color

(2) setContentIntent jump intention after clicking the notification

We set up a jump interface. In the new class, we need to "register" (otherwise, we won't jump or report an error. It's annoying)

5, More details are presented in the instance notes


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=""
<!--    Method name is sendNote,Shortcut key jump directly in java Specific implementation in-->
        android:text="give an announcement" />

        android:text="Cancellation notice" />


public class MainActivity extends AppCompatActivity {

    //Create a global object: NotificationManager
    private NotificationManager manager;
    //Create a global object: notification
    private Notification notification;

    protected void onCreate(Bundle savedInstanceState) {

        //Use the getSystemService method to get the NotificationManager object
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        //If it is 8.0 or above, we can create this object; (I stepped on the pit before, and got a 4.0 version of the project. I couldn't get the notice)
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            NotificationChannel channel = new NotificationChannel("zhua", "Test notification",NotificationManager.IMPORTANCE_HIGH);

            //Created how channel uses it: use the notification management class of NotificationManager
            manager.createNotificationChannel(channel);//Bind channelid to manager
        //Create jump intent
        Intent intent = new Intent(this,NotificationActivity.class);
        //setContentIntent parameter to be passed in jump intention: pendingIntent 
        PendingIntent pendingIntent = PendingIntent.getActivities(this, 0, new Intent[]{intent}, 0);

        //Set the attribute of notification directly through the chain structure (set the attribute of notification)
        notification = new NotificationCompat.Builder(this,"zhua")
                //Title (required)
                .setContentTitle("official communication")
                //Notice content (required)
                .setContentText("Hua Tianzhu is here")
                //Notification small icon (required) this picture cannot be RGB

                //Large icon for notification: this parameter requires the bitmap type of the picture, so BitmapFactory conversion is required
				//Set the notification icon bar icon to red: setColor the incoming parameter is argb
                //Instead of ordinary intent, pandding intent is created on it
                //After clicking the notification, the notification will be cancelled

    public void sendNote(View view) {
        //Two parameters, the first is id. click in and find that there are no requirements. Just write a 1

    public void canselNote(View view) {
        manager.cancel(1); //The id of this cancellation notice should correspond to the one above

//For the purpose of jump, the class created here must be associated with alt+enter in this place
public class NotificationActivity extends Activity {
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        //Make a simple print to see the effect
        Log.e("zhua", "onCreate: get into NotificationActivity" );