How to use Android notification bar

  • 2021-12-12 09:57:36
  • OfStack

Directory 1. Set up notification content 2. Create channels 3. Set the click action of the notification bar Step 4 Display notifications

1. Set up notification content


	//CHANNEL_ID , channel ID , Android 8.0 Versions and higher must be set 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
    		// Set small icons 
            .setSmallIcon(R.drawable.notification_icon)
            // Set the title 
            .setContentTitle(textTitle)
            // Setting content 
            .setContentText(textContent)
            // Set the level 
            .setPriority(NotificationCompat.PRIORITY_DEFAULT);

STEP 2 Create channels

Providing notifications on Android 8.0 and later requires the application's notification channel to be registered in the system.


    private void createNotificationChannel() {
        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 will affect how notifications are displayed 
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
            channel.setDescription(description);

            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }

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

3. Set the click action of the notification bar

Clicking on 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);
    //1 Click notifications are opened in a separate interface to avoid adding to existing activity Stack, you can set the following startup mode 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    // Create activity Type of pendingIntent 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)
            // Settings pendingIntent
            .setContentIntent(pendingIntent)
            // Set whether it disappears automatically after clicking 
            .setAutoCancel(true);    

Step 4 Display notifications


    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    //notificationId  Equivalent to notification only 1 Identity for updating or removing notifications 
    notificationManager.notify(notificationId, builder.build());

There are many special functions, which can be set directly by viewing the official website tutorial.

The above is the Android notification bar of the use of the details, more information about the use of Android notification bar please pay attention to other related articles on this site!


Related articles: