Notification prompt dialog box in Android

  • 2021-01-06 00:44:00
  • OfStack

Notification is a global notification that is displayed at the top of the screen. When the user swipes down, the notification is displayed as an icon.

1. Notification controls for displaying notifications on the status bar. Notification controls are not the same on different devices

2. Basic layout of Notification

Element composition:

Icon/Photo: large icon Tiltle/Name: header Message: content message Timestamp: notification time. By default, the notification time is issued by the system. You can also set secondary/Icon small icon via setWhen ()

3. The basic process of using Notification

The status notification bar mainly involves two classes: Notification and NotificationManager

ES38en: Notification information class, which corresponds to the properties of the notification bar

NotificationManager: is the status bar notification management class, responsible for sending notification, clear notification and other operations

step1: Object NotificationManager manager = (NotificationManager) getSystemService (NOTIFICATION_SERVICE);

step2: Create an Builder construct with 1 notification bar, Notification.Builder builder = new Notification.Builder (this);

ES69en3: Set Builder relevant Settings, such as title, content, icon action

step4: Assign a value to notification by calling the build() method of Builder

step5: Call the notify() method of NotificationManager to send a notification

We can also cancel the notification by calling the cancel () method of ES87en

4. Set up some relevant methods

Notification.Builder mBuilder = new Notification.Builder(this);

Then call the following related methods to set, the common methods are as follows:

The & # 8226; setContentTitle(CharSequence) : Set the title
The & # 8226; setContentText(CharSequence) : Settings content
The & # 8226; setSubText(CharSequence) : Sets 1 small line of text below the content
The & # 8226; setTicker(CharSequence) : Sets the text message to be displayed at the top when a notification is received
The & # 8226; setWhen(long) : Set the notification time. System.currentTimeMillis() when the notification is received.
The & # 8226; setSmallIcon(int) : Set the small icon in the lower right corner, which will also be displayed at the top when notifications are received
The & # 8226; setLargeIcon(Bitmap) : Sets the large icon on the left
The & # 8226; setAutoCancel(boolean) : Does the user cancel the notification after clicking the Notification panel (default does not cancel)
The & # 8226; setDefaults(int) : The easiest way to add sound, flash and vibration effects to notifications, using the default (defaults) property. Multiple properties can be combined.
Notification.DEFAULT_VIBRATE(add default vibration reminder);
Notification.DEFAULT_SOUND(add default sound reminder);
Notification.DEFAULT_LIGHTS(Added default 3 color light reminder)
Notification.DEFAULT_ALL (add default all 3 above reminders)
setVibrate(long[]) : Set vibration mode, for example:

setVibrate (new long [] {0300500700}); Delay 0ms, then vibrate 300ms, delay 500ms, then vibrate 700ms again, more on Vibrate usage later!

setLights(int argb, int onMs, int offMs) : Set 3 color lights, the parameters are: light color, light duration, dark time, not all colors, this depends on the device, some mobile phones do not have 3 color lights; flags is Notification.FLAG_SHOW_LIGHTS to support 3 color light reminder!

setSound(Uri) : Set the ring tone when receiving a notification, either by the system or by yourself. Examples are as follows:

.setDefaults (Notification.DEFAULT_SOUND) // Get the default ringtone
setSound (Uri. parse (" file: / / / / sdcard xx/xx mp3 ")) / / to get custom ringtones
.setSound(Uri.withAppendedPath (Audio.Media.INTERNAL_CONTENT_URI, "5")) // Get the ring tones from the Android library

setOngoing(boolean) : Set to ture to indicate that it is an ongoing notification. They are usually used to indicate a background task that the user is actively involved in (e.g., playing music) or is waiting in some way and therefore occupying the device (e.g., a file download, synchronous operation, active network connection).

setProgress (int, int boolean) : set with a progress bar to inform parameters in the order: the progress bar maximum value, the current progress, progress is not sure if in order to determine the progress bar: call setProgress (max progress, false) to set up the notice, at the time of update progress in this update notification progress, and to remove the progress bar after the download is complete, by calling the setProgress (0, 0, false). Call setProgress(0, 0, true). At the end of the operation, call setProgress(0, 0, false) and update the notification to remove the indicator bar

setContentIntent(PendingIntent) : PendingIntent and Intent slightly different, it can set the number of execution, mainly used in remote service communication, alarm, notification, launcher, short message, in 1 case is less used. Activity: getActivity(Context, int, Intent, int), of course, you can also start Service or Broadcast bit identifier (fourth parameter) :

FLAG_ONE_SHOT means that the returned PendingIntent can only be executed once, and will be cancelled automatically after completion of execution
FLAG_NO_CREATE means that if the described PendingIntent does not exist, the corresponding PendingIntent is not created, but NULL is returned
FLAG_CANCEL_CURRENT indicates that the corresponding PendingIntent already exists, so cancel the former and create a new PendingIntent, which is good for keeping the data up to date and can be used for instant messaging
FLAG_UPDATE_CURRENT represents the updated PendingIntent

Examples of use:

// Click to jump Activity
Intent intent = new Intent(context,XXX.class); 
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 

setPriority(int) : Set priority:


The user


An important and urgent notification notifies the user that the event is time-critical or requires immediate attention.


High priority is used for important communications, such as short messages or chats, that are of interest to the user.


The default priority is used for advice that does not have a special priority classification.


Low priority can notify the user of events that are not very urgent.


Used for background messages (such as weather or location information). The lowest-priority notifications will only show ICONS in the status bar, and only the user can pull down the notification drawer to see the content.

Corresponding property: Notification. PRIORITY_HIGH..

5. Basic use cases

package com.example.test3;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements View.OnClickListener{
private Button btn1;
private Button btn2;
//  Two related classes 
private NotificationManager manager;
private Notification notification;
private static final int NOTIFYID_1 = 1;
protected void onCreate(Bundle savedInstanceState){
btn1 = (Button) findViewById(;
btn2 = (Button) findViewById(;
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
public void onClick(View view) {
switch (view.getId()){
//  define 1 a PendingIntent, Click on the Intent You can start 1 A new one Intent
Intent intent = new Intent(MainActivity.this,OtherActivity.class);
PendingIntent pit =PendingIntent.getActivity(MainActivity.this,0,intent,0);
//  Set picture text prompt mode and so on 
Notification.Builder builder = new Notification.Builder(MainActivity.this);
builder.setContentTitle(" Ye Liangchen ") // The title 
.setContentText(" I have a 1 There are a hundred ways you can't stay ~") // content 
.setSubText(" -- Remember my name is Ye Liangchen ") // Content below 1 Small paragraph 
.setTicker(" Received Ye Liangchen sent over the information ~") // The text message displayed in the status bar after receiving the message 
.setWhen(System.currentTimeMillis()) // Set Notification Time 
.setSmallIcon(R.mipmap.ic_account) // Set small ICONS 
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE) // Set the default 3 Color light and vibrator 
.setAutoCancel(true) // Settings click and cancel Notification
notification =;

Related articles: