Original address: http://android.xsoftlab.net/training/notify-user/index.html
Introduction
Notifications are used to present things to the user in a more convenient way when an event occurs. Users can interact directly with the notification at their convenience.
The Notifications Design Guide course tells you about designing effective notifications and when to use them. This lesson will learn how to implement a common notification design.
Build Notifications
The implementation of this class is mainly based on the Notificationcompat.builder class, and the Notificationcompat.builder class belongs to the support library. Developers should use Notificationcompat and its subclasses, especially Notificationcompat.builder, to support a broader platform.
Creating the Notification Builder
When you create a notification, you specify the UI content of the notification and its click behavior. A Builder object must contain at least the following conditions:
- A small icon, set by the Setsmallicon () method.
- Notification header, set by the Setcontenttitle () method.
- Detailed text, set by the Setcontenttext () method.
Like what:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!");
Defining the behavior of notifications
When you create a notification, you should add at least one behavior to the notification. This behavior takes the user to the activity, which shows in detail what is going on or allows the user to take further action. Inside the notification, the behavior is specified by the intent contained by Pendingintent, which can be used to start the activity.
How you construct pendingintent depends on the type of activity you want to start. When the activity is started by notification, the developer must consider the navigation experience that the user is looking for. In the following code, clicking on a notification initiates a new activity that inherits the behavior of the notification. In this case, you do not need to create an artificial fallback stack.
new Intent(this, ResultActivity.class);...// Because clicking the notification opens a new ("special") activity, there‘s// no need to create an artificial back stack.PendingIntent resultPendingIntent = PendingIntent.getActivity( this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Set the click behavior of Notifications
In order for the pendingintent to be associated with a gesture, the corresponding method of Notificationcompat.builder is called. For example, to start an activity, call the Setcontentintent () method to add Pendingintent.
Publish Notifications
The following steps need to be performed to publish a notification:
- Get an instance of Notificationmanager.
- Use the Notify () method to publish the notification. The ID of the notification needs to be specified when calling the Notify () method, which is used to update the notification later.
- Call the Build () method, which returns a notification object.
NotificationCompat.Builder mBuilder;...// Sets an ID for the notificationint001;// Gets an instance of the NotificationManager serviceNotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);// Builds the notification and issues it.mNotifyMgr.notify(mNotificationId, mBuilder.build());
Android Official Development Document Training Series Course Chinese version: Notify users of Build notifications