From: Android APP_ Control (6) -- Notification notification
Author: and pursung
Release time: 2021-04-02 00:30:14
website: https://blog.csdn.net/weixin_44742824/article/details/115382674
Effect list
Implementation effect: click send notification, and the system sends a notification; You can jump to view through the drop-down taskbar; You can also directly click the message to jump to view; You can also click the button to cancel the notification. As shown below:
1, Create a notification manager
NotificationManager class is a notification manager class. This object is a service maintained by the system and is obtained in singleton mode. Therefore, this object is generally not instantiated directly.
In the Activity, you can use the getSystemService method to obtain the NotificationManager object, which can return the corresponding object through the handle of the Android system level service. You need to return the notification manager here, so you can pass the notification directly_ Service is enough.
Specifically:
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- 1
NotificationManager is used for format conversion
2, Use the Builder constructor to create a Notification object
Using the Builder constructor of NotificationCompat class to create Notification objects can ensure that the program works normally on all versions. Android8.0 adds the concept of Notification channel. If it is not set, the Notification cannot be in Android 8.0 0 is displayed on the machine.
Notification notification = new NotificationCompat.Builder(this,"zhua")
- 1
context: indicates the environment
3, Notification channel: NotificationCannel
Android8.0 introduces notification channels, which allow you to create user customizable channels for each type of notification you want to display.
Three important parameters:
(1) id: channelld, which is the channel id
(2) name: Information
(3) Importance: importance of notification
Notification importance is set in the NotificationManager class:
parameter | notice | eject | Prompt tone | status bar |
---|---|---|---|---|
NONE | × | × | × | × |
MIN | √ | × | × | × |
LOW | √ | × | × | √ |
DEFAULT | √ | × | √ | √ |
HIGH | √ | √ | √ | √ |
4, Set the properties of notification through chain structure
That is to set the notification properties for.
The specific attributes are described in detail in the following example code. Two error prone points are recorded below.
(1) setSmallIcon should pay attention to:
From Android 5 At the beginning of the 0 system, the design of the notification bar icon has been modified. Now Google requires that the notification bar icon of all applications should only be drawn with alpha layer, not RGB layer.
Learn to look at the parameters. For example, the following one requires you to pass in the rbg parameter to set the color
(2) setContentIntent jump intention after clicking the notification
We set up a jump interface. In the new class, we need to "register" (otherwise, we won't jump or report an error. It's annoying)
5, More details are presented in the instance notes
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- Method name is sendNote,Shortcut key jump directly in java Specific implementation in--> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="sendNote" android:text="give an announcement" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="canselNote" android:text="Cancellation notice" /> </LinearLayout>
MainAvtivity.java
public class MainActivity extends AppCompatActivity { //Create a global object: NotificationManager private NotificationManager manager; //Create a global object: notification private Notification notification; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Use the getSystemService method to get the NotificationManager object manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //If it is 8.0 or above, we can create this object; (I stepped on the pit before, and got a 4.0 version of the project. I couldn't get the notice) if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ NotificationChannel channel = new NotificationChannel("zhua", "Test notification",NotificationManager.IMPORTANCE_HIGH); //Created how channel uses it: use the notification management class of NotificationManager manager.createNotificationChannel(channel);//Bind channelid to manager } //Create jump intent Intent intent = new Intent(this,NotificationActivity.class); //setContentIntent parameter to be passed in jump intention: pendingIntent PendingIntent pendingIntent = PendingIntent.getActivities(this, 0, new Intent[]{intent}, 0); //Set the attribute of notification directly through the chain structure (set the attribute of notification) notification = new NotificationCompat.Builder(this,"zhua") //Title (required) .setContentTitle("official communication") //Notice content (required) .setContentText("Hua Tianzhu is here") //Notification small icon (required) this picture cannot be RGB .setSmallIcon(R.drawable.ic_baseline_account_box_24) //Large icon for notification: this parameter requires the bitmap type of the picture, so BitmapFactory conversion is required .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.head)) //Set the notification icon bar icon to red: setColor the incoming parameter is argb .setColor(Color.parseColor("#ff0000")) //Instead of ordinary intent, pandding intent is created on it .setContentIntent(pendingIntent) //After clicking the notification, the notification will be cancelled .setAutoCancel(true) .build(); } public void sendNote(View view) { //Two parameters, the first is id. click in and find that there are no requirements. Just write a 1 manager.notify(1,notification); } public void canselNote(View view) { manager.cancel(1); //The id of this cancellation notice should correspond to the one above } }
NotificationActivity.java
//For the purpose of jump, the class created here must be associated with alt+enter in this place public class NotificationActivity extends Activity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Make a simple print to see the effect Log.e("zhua", "onCreate: get into NotificationActivity" ); } }