SoFunction
Updated on 2025-03-01

Android programming notification bar usage summary

This article summarizes the usage of notification bar in Android programming. Share it for your reference, as follows:

I haven't used the notification function of Android for a long time. Today I took out the code from two years ago and found that many methods were discarded, and I felt very uncomfortable watching various deleted lines in the code. So, open Google, view the official documents, and learn the latest method of sending notification messages.

The code in this article is written in accordance with Google's official documentation:

/guide/topics/ui/notifiers/

1. First, obtain the system notification service:

Copy the codeThe code is as follows:
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

2. Send a simplest notification

public void simpleNotice(View view) {
    //This Builder is in., the same below.    Builder mBuilder = new Builder(this);
    //The text displayed on the notification bar when the system receives the notification.    ("Tianjin, sunny, 2-15 degrees, breeze");
    //The small icon displayed on the notification bar    (.consult_answer);
    //Notification title    ("Weather Forecast");
    //Notification content    ("Tianjin, sunny, 2-15 degrees, breeze");
    //Set a large icon, that is, the picture on the left side of the notification bar (if only a small icon is set, a small icon will be displayed here)    ((getResources(), .share_sina));
    //The number displayed on the left side of the small icon    (6);
    //Set to non-clearable mode    (true);
    //Display notifications, the id must not be repeated, otherwise the new notification will overwrite the old notification (using this feature, the notification can be updated)    (1, ());
}

3. Delete a notification. The parameter is the id of the notification

(1);

4. Send a notification, click the notification and jump to an Activity. After returning from this Activity, enter a certain page in the program (usually the homepage)

//Click the notification to enter an activity, and when clicking back, enter the specified page.public void resultActivityBackApp(View view) {
    Builder mBuilder = new Builder(this);
    ("Notice Title 2");
    (.ic_launcher);
    ("Notice Title 2");
    ("Click the notification to enter an activity, and click back to enter the specified page.");
    //Set to disappear after clicking once (if there is no click event, this method is invalid.)    (true);
    //The page that needs to be redirected after clicking the notification    Intent resultIntent = new Intent(this, );
    //Use TaskStackBuilder to set the return relationship for "notification page"    TaskStackBuilder stackBuilder = (this);
    //Set the page that opens after clicking the notification Return to the page.  (Specified in manifest)    ();
    (resultIntent);
    PendingIntent pIntent = (0, PendingIntent.FLAG_UPDATE_CURRENT);
    (pIntent);
    // mId allows you to update the notification later on.
    (2, ());
}

At the same time, you need to specify the parent Activity in manifest for the activity that opens after clicking notification.

<activity
  android:name=".ResultActivityBackApp"
  android:parentActivityName=".MainActivity">
  <meta-data
    android:name=".PARENT_ACTIVITY"
    android:value=".MainActivity" />
</activity>

(The property parentActivityName of the activity is the property in API 16, and the code in meta-data is compatible with API 16 or below. Therefore, for most programs, both places have to be written.)

5. Similar to the above 4, except that when you return to the home page when you return in the open Activity

//Click the notification to enter an activity, and return to the desktop when clicking backpublic void resultActivityBackHome(View view) {
    Builder mBuilder = new Builder(this);
    ("Notice Title 3");
    (.ic_launcher);
    ("Notice Title 3");
    ("Click the notification to enter an activity, click back to the desktop");
    //Set to disappear after clicking once (if there is no click event, this method is invalid.)    (true);
    Intent notifyIntent = new Intent(this, );
    (Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pIntent = (this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    (pIntent);
    (3, ());
}

6. Notification with progress bar

public void progressNotice(View view) {
    final Builder mBuilder = new Builder(this);
    ("Notice Title 4");
    ("Picture Download")
        .setContentText("Download in progress")
        .setSmallIcon(.ic_launcher);
    // Start a lengthy operation in a background thread
    new Thread(new Runnable() {
      @Override
      public void run() {
        int progress;
        for (progress = 0; progress &lt;= 100; progress++) {
          // Sets the progress indicator to a max value, the current completion percentage,
          // and "determinate" state
          (100, progress, false);
          //Progress bar for unclear progress//          (0, 0, true);
          (4, ());
          // Analog delay          try {
            (200);
          } catch (InterruptedException e) {
            ();
          }
        }
        // When the loop is finished, updates the notification
        ("Download complete");
        // Removes the progress bar
        (0, 0, false);
        (4, ());
      }
    }
    ).start();
}

7. Notice of extended layout. Press and hold the notification bar to slide to view more detailed content

public void expandLayoutNotice(View view) {
    Builder mBuilder = new Builder(this);
    ("Notice Title 5");
    (.ic_launcher);
    ("Notice Title 5");
    ("Press and hold the notification drop-down to display the extended layout");
     inboxStyle = new ();
    String[] events = new String[]{"Beijing", "Tianjin", "Shanghai", "Guangzhou"};
    // Set the title of the extended layout    ("Event tracker details:");
    for (String s : events) {
      (s);
    }
    (inboxStyle);
    (5, ());
}

8. The notification bar of the custom layout. (This is not recommended according to Google's official documentation, because there are too many factors to consider when using this method to adapt different screens. Moreover, the notification bar should show the most concise information, which is enough for the default layout of most programs.)

//Notification of custom layoutpublic void customLayoutNotice(View view) {
    Builder mBuilder = new Builder(this);
    ("Notice Title 6");
    ("Notice Title 6");
    (.ic_launcher);
    RemoteViews remoteViews = new RemoteViews(getPackageName(), .custom_layout_notice);
    (remoteViews);
    //Set text for buttons on RemoteViews    (.custom_layout_button1, "setText", "Button1");
    (.custom_layout_button2, "setText", "Button2");
    //Set click events for buttons on RemoteViews    Intent intent1 = new Intent(this, );
    ("content", "From button1 click!");
    PendingIntent pIntentButton1 = (this, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
    (.custom_layout_button1, pIntentButton1);
    Intent intent2 = new Intent(this, );
    ("content", "From button2 click!");
    PendingIntent pIntentButton2 = (this, 1, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
    (.custom_layout_button2, pIntentButton2);
    (6, ());
}

For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Android multimedia operation skills summary (audio, video, recording, etc.)》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.