Use Notification in Android to implement the normal Notification bar (Notification Example 1), and implement the rest example in java.
Notification is a message displayed outside of your application's general interface. When the app asks the system to send a message, the message is first displayed in the notification bar in a chart. To view the details of a message, go to the notification drawer. Icationicationdrawer is controlled at the system level. You can view it at any time, not limited to apps.
Notification design:
As an important part of android UI, notification has its own design guidelines.
The Notification interface elements can be displayed in the notification drawer in two ways, depending on your android version and icationicationdrawer status.
Two display modes of Notification:
(1) Common View
This style is the standard display mode of notification drawer.
(2) wide view
It indicates that a larger view is displayed when your notification is expanded. This style is new only after android4.1.
The following describes the implementation of common views in detail:
The notification value is up to 64dp in a general view. Even if you create a wide view notification, it is displayed in normal size without being expanded. The following is a common notification.
The blue box indicates the following meanings:
1. Title
2. Large icons
3. Notification content
4. Notification data
5. small icons
6. release time of Notification.
You can call setWhen () to set a specific time,
The default value is the time when the system receives the notification.
The following is our demo:
In this example, the page Jump effect is added based on the common view.Notification action and behavior:
Although this is optional, you should add at least one behavior for your notification: allow users to click notification to enter an activity for more viewing or subsequent operations. A notification can provide multiple actions, and you should also allow the user to always have corresponding response actions after clicking a notification, usually to open an activity. You can also add a button in the notification to respond to the click event, such as a delay alarm, or immediately reply to a short message.
Within notification, an action is defined in a PendingIntent, which contains an intent used to start the activity in your app. To associate PendingIntent with a gesture, you need to call the appropriate icationicationcompat. Builder method.
For example, if you want to start the activity when you click the notification text, you need to call setContentIntent () of icationicationcompat. Builder to add PendingIntent. Starting an activity is the most common type of notification action response.
Step 1: Layout
Activity_main.xml of (only set the trigger button ):
1 <? Xml version = "1.0" encoding = "utf-8"?>
2 <LinearLayout
3 xmlns: android = "http://schemas.android.com/apk/res/android"
4 xmlns: tools = "http://schemas.android.com/tools"
5 android: id = "@ + id / activity_main"
6 android: layout_width = "match_parent"
7 android: layout_height = "match_parent"
8 tools: context = "com.example.administrator.day12.MainActivity">
9 <Button
10 android: text = "Show notifications"
11 android: layout_width = "match_parent"
12 android: layout_height = "wrap_content"
13 android: id = "@ + id / button"
14 android: onClick = "show1" />
15 </ LinearLayout>
Step 2: Layout
OfRedirect page activity_content.xml(Only set display text ):
1 <? Xml version = "1.0" encoding = "utf-8"?>
2 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
3 xmlns: tools = "http://schemas.android.com/tools"
4 android: id = "@ + id / activity_content"
5 android: layout_width = "match_parent"
6 android: layout_height = "match_parent"
7 tools: context = "com.example.administrator.day12.ContentActivity">
8 <TextView
9 android: layout_width = "match_parent"
10 android: layout_height = "match_parent"
11 android: gravity = "center"
12 android: textSize = "30sp"
13 android: text = "Ten wins and ten defeats" />
14 </ LinearLayout>
Step 3:
Java code MainActivity. java:
1 import android.app.Notification;
2 import android.app.NotificationManager;
3 import android.app.PendingIntent;
4 import android.content.Context;
5 import android.content.Intent;
6 import android.graphics.BitmapFactory;
7 import android.support.v7.app.AppCompatActivity;
8 import android.os.Bundle;
9 import android.support.v7.app.NotificationCompat;
10 import android.view.View;
11 import android.widget.RemoteViews;
12 public class MainActivity extends AppCompatActivity {
13 private static final int NO_1 = 0x1;
14 @Override
15 protected void onCreate (Bundle savedInstanceState) {
16 super.onCreate (savedInstanceState);
17 setContentView (R.layout.activity_main);
18}
19 public void show1 (View v) {
20 NotificationCompat.Builder builder = new NotificationCompat.Builder (this);
21 builder.setSmallIcon (R.mipmap.guojia);
22 builder.setContentTitle ("Guo Jia");
23 builder.setContentText ("Let's hit Yuan Shao");
24 // Set Notification.Default_ALL (default to enable all services (breathing lights, ringtones, etc.)
25 builder.setDefaults (Notification.DEFAULT_ALL);
26 // Call setContentIntent () of NotificationCompat.Builder to add PendingIntent
27 Intent intent = new Intent (this, ContentActivity.class);
28 intent.putExtra ("info", "Guo Jia sent you a plan!");
29 PendingIntent pi = PendingIntent.getActivity (this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
30 builder.setContentIntent (pi);
31 // Get Notification
32 Notification n = builder.build ();
33 // Get Notification object by NotificationCompat.Builder.build ()
34 NotificationManager manager = (NotificationManager) getSystemService (Context.NOTIFICATION_SERVICE);
35 // Then call NotificationManager.notify () to transfer to the system
36 manager.notify (NO_1, n);
37}
38}
Step 4:
Java (Activity after jump)Function Code Implementation ContentActivity. java (only Tusi ):
1 public class ContentActivity extends AppCompatActivity {
2 @Override
3 protected void onCreate (Bundle savedInstanceState) {
4 super.onCreate (savedInstanceState);
5 setContentView (R.layout.activity_content);
6 // Get toast content by getting putExtra set in MainActivity
7 Toast.makeText (this, getIntent (). GetStringExtra ("info"), Toast.LENGTH_SHORT) .show ();
8 }
9 }
This is the code for demonstration. Let's sort out the Implementation ideas:
(1) trigger the click event through the button
(2) assign some UI information and related actions of notification to icationicationcompat. builder object, and then use icationicationcompat. builder. build () to obtain the notification object itself; then call icationicationmanager. notify () transfers this notification to the system.
(3) in step 2, Use setContentIntent () of Builder to add PendingIntent and add behavior for Notification, that is, Activity jump.
(4) effect of setting the opened Activity.