Android-Notification

“This is the fifth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

When an application is running in the background and wants to give the user some hints to learn, it needs to use Notification. When a notification is sent, a notification icon is displayed in the status bar at the top of the phone. The details of the notification can be seen by pulling down the status bar.

We need a NotificationManager to manage notifications. We can call the getSystemService () method of the Context. The getSystemService () method takes a string argument to determine which service to get from the system. Here we pass context.notification_service. Therefore, getting an instance of NotificationManager can be written as:

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Copy the code

The next step is to use a Builder to create a Notification object, but the problem is that notifications are modified in almost every version of Android, and API instability is particularly pronounced in notifications. The support-V4 library provides a NotificationCompat class that uses the constructor to create Notification objects. We can guarantee that our application will work properly on all Android system versions.

The code for creating a Notification object is as follows:

Notification notification = new NotificationCompat.Builder(context).build();
Copy the code

Here is a code that shows how to create a notification:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button sendNotice = (Button) findViewById(R.id.send_notice);
    sendNotice.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.send_notice:
                    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                    Notification notification = new NotificationCompat.Builder(this)
                            .setContentTitle("This is content title")
                            .setContentText("This is content text")
                            .setWhen(System.currentTimeMillis())
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                            .build();
                    manager.notify(1,notification);
                    break;
                default:
                    break; }}}); }Copy the code

There are five setup methods called in the above code, and we’ll examine each of them. The setContentTitle () method is used to specify the content of the notification’s title, which can be seen by pulling down the system status bar. The setContentText () method is used to specify the governing body content, which can also be seen by pulling down the system status bar. The setWhen () method is used to specify when the notification was created, in milliseconds, and when the notification is pulled down, the specified time is displayed on the notification. The setSmallIcon () method is used to set the small icon of the notification. Note that this can only be set using the image of the pure Ailha layer. The small icon will be displayed on the system notification bar. The setLargeIcon () method sets the large icon for the notification, which is visible when you pull down the system notification bar.

The notification is then displayed by calling notify () of the NotificationManager. The notify () method takes two parameters. The first parameter is id, and the second parameter is a Notification object, so we pass in the Notification object we just created.