NotificationSend a notification

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

   <Button
       android:text="General notification"
       android:onClick="normal"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"/>

   <Button
       android:text="group notification"
       android:onClick="groupNotify"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"/>

   <Button
       android:text="progress bar notification"
       android:onClick="progressNotify"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"/>

   <Button
       android:text="custom notification"
       android:onClick="divNotify"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"/>

</LinearLayout>
package com.bawei.day3_notificationdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.NotificationTarget;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R. layout. activity_main);

    }

    //General notification
    public void normal(View view){
        // Get the notification manager, because it is a system service, so it needs to be forced
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // The object to get the notification is a builder, chain programming structure, creator mode
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);//Set the notification small icon, must be set
        builder.setContentTitle("Notification");//Title
        builder.setContentText("I am a normal message notification");//Content
        builder.setAutoCancel(true);//Set whether to delete
        //builder.setLargeIcon();//Set large icon
        //Set the notification effect Notification.DEFAULT_ALL means all, Notification.DEFAULT_LIGHTS light Notification.DEFAULT_SOUND sound Notification.DEFAULT_VIBRATE vibration
        builder.setDefaults(Notification.DEFAULT_ALL);

        Intent intent = new Intent(this, MainActivity. class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,101,intent,PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(pendingIntent);//Set the intent to make the notification jump



        Notification notification = builder. build();
        //send notification
        notificationManager.notify(1, notification);//or report an error, remember to add notification permissions
    }

    // group notification
    public void groupNotify(View view){
        // Get the notification manager, because it is a system service, so it needs to be forced
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //Get the notification object, which is a builder, chain programming structure, creator mode
        Notification builder1 = new Notification. Builder(this)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentTitle("Notice 1")
        .setContentText("Notification 1 content")
        .setGroup("a")//Group notification key position, set the notification group
        .setGroupSummary(true)//Whether to overlap, do not display, fold at the bottom
        .build();

        Notification builder2 = new Notification. Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Notice 2")
                .setContentText("Notification 2 content")
                .setGroup("a")//Group notification key position, set the notification group
                .setGroupSummary(true)//Whether to overlap, do not display, fold at the bottom
                .build();

        Notification builder3 = new Notification. Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Notice 3")
                .setContentText("Notification 3 content")
                .setGroup("b")//Group notification key position, set the notification group
                .setGroupSummary(true)//Whether to overlap, do not display, fold at the bottom
                .build();

        Notification builder4 = new Notification. Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Notice 4")
                .setContentText("Notification 4 content")
                .setGroup("b")//Group notification key position, set the notification group
                .setGroupSummary(true)//Whether to overlap, do not display, fold at the bottom
                .build();

        notificationManager. notify(1, builder1);
        notificationManager. notify(2, builder2);
        notificationManager. notify(3, builder3);
        notificationManager. notify(4, builder4);
    }

    private Timer timer;
    //Progress bar notification, mainly need to use timer or thread to update notification
    public void progressNotify(View view){
        // Get the notification manager, because it is a system service, so it needs to be forced
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        Notification.Builder builder = new Notification.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Download")
                .setProgress(100,0,false);//The maximum value of the parameter progress bar, the current progress, whether to display the progress indistinctly
        notificationManager. notify(6, builder. build());

        timer = new Timer();
        timer.schedule(new TimerTask() {
            int progress = 0;
            @Override
            public void run() {
                if (progress == 100){
                    notificationManager. cancel(6);
                    timer. cancel();
                } else {
                    builder.setProgress(100,progress += 10,false);
                    notificationManager. notify(6, builder. build());
                }
            }
        },0,1000);
    }

    //custom notification
    public void divNotify(View view){
        //Get the notification manager
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //Use the remote View to load the layout
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.diy);
        //Modify the remote text and assign a value to the text
        remoteViews.setTextViewText(R.id.tv,"You are so beautiful");
        //Load a local image, the first parameter is the id, the second parameter is the local image that needs to be replaced or loaded
        remoteViews.setImageViewResource(R.id.play,R.drawable.ic_pause);
        //Set the local image in the remote layout
        Notification notification = null;
        //Use setCustomContentView
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            notification = new Notification. Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setCustomContentView(remoteViews)///Load remote layout
                    .build();
        }
        //send notification
        notificationManager. notify(7, notification);

        //load network image
        //parameters, context, location where images need to be loaded, remote views (remoteViews), current notification, notification id
        NotificationTarget notificationTarget = new NotificationTarget(this,R.id.iv,remoteViews,notification,7);
        Glide.with(this).asBitmap().load("http://43.143.146.165:8080/class5server/image/1.jpg").into(notificationTarget);
    }

    //Double click the return key to exit
    private long first;//Record the time of the first click

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent. KEYCODE_BACK){
            //Find the returned key value, through keyCode, you can get whether the current click is the return key
            //The error between two clicks is 2 seconds, it is considered to be a connection, and you can exit the app, otherwise it will prompt "click again to exit the app"
            long second = System.currentTimeMillis();//The time of the second click
            if (second - first > 2000){
                first = second;//Assign the time of the second click to the first time for the next comparison
                Toast.makeText(this, "Click again to exit the app", Toast.LENGTH_SHORT).show();
                return true;
            }
        }

        return super.onKeyDown(keyCode, event);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<!--Custom notification-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@ + id/iv"
        android:padding="10dp"
        android:src="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <LinearLayout
        android:padding="15dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@ + id/tv"
            android:text="song name"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">

            <ImageView
                android:id="@ + id/previous"
                android:src="@drawable/ic_previous"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"/>

            <ImageView
                android:id="@ + id/play"
                android:src="@drawable/ic_play"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"/>

            <ImageView
                android:id="@ + id/next"
                android:src="@drawable/ic_next"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"/>

        </LinearLayout>

    </LinearLayout>



</LinearLayout>