The notification bar can be created in different versions. Currently, it is recommended to use the NotificationCompat API, which is compatible with Android 4.0. However, some new functions, such as inline reply operations, are not supported in older versions.

1. Set the notification content

	//CHANNEL_ID, channel ID, Android 8.0 or later must be set
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
    		// Set the small icon
            .setSmallIcon(R.drawable.notification_icon)
            // Set the title
            .setContentTitle(textTitle)
            // Set the content
            .setContentText(textContent)
            // Set the level
            .setPriority(NotificationCompat.PRIORITY_DEFAULT);
Copy the code

Second, create channels

To provide notifications on Android 8.0 and later, you need to register your app’s notification channel with the system.

    private void createNotificationChannel(a) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = getString(R.string.channel_name);
            String description = getString(R.string.channel_description);
            // Different levels of importance affect how notifications are displayed
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = newNotificationChannel(CHANNEL_ID, name, importance); channel.setDescription(description); NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); }}Copy the code

The above code should be executed immediately when the Application starts and can be initialized in the Application.

Three, set the notification bar click operation

Generally, clicking the notification bar will open the corresponding Activity interface. The specific code is as follows:

	// The interface you want to open when you click
    Intent intent = new Intent(this, AlertDetails.class);
    // Click notifications to open a separate screen. To avoid adding them to the existing activity stack, you can set the following startup mode
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    // Create pendingIntent as an activity. You can also create other components such as broadcasts
    PendingIntent pendingIntent = PendingIntent.getActivity(this.0, intent, 0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.drawable.notification_icon)
            .setContentTitle("My notification")
            .setContentText("Hello World!")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            / / set the pendingIntent
            .setContentIntent(pendingIntent)
            // Set whether to disappear automatically after being clicked
            .setAutoCancel(true);    
Copy the code

Display notifications

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    //notificationId is the unique identifier of a notification and is used to update or remove the notification
    notificationManager.notify(notificationId, builder.build());
Copy the code

There are also many special functions, you can directly check the official website tutorial to set up.