Android Notification (notification)

When using Android APP, we want to receive a notification from an APP running in the background. The notification icon appears on the status bar above the phone, and the detailed information will be displayed after being pulled down. This function is very common and meaningful. It not only allows users to subscribe to their favorite The notification can also make a good recommendation for the APP.

In Android, notifications mainly use the NotificationManager class and the Notification class.

//通知管理类来对通知进行管理
 NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        
//使用Builder构造器来创建Notification对象
Notification notification = new NotificationCompat.Builder()
                .setContentTitle()
                .setContentText()
                .setWhen()
                .setSmallIcon()
                .setLargeIcon()
                .build();

//让通知显示出来
 notificationManager.notify(1,notification);  //params1是id 要保证每一个通知指定的id都是不同的

setContentTitle() //Set notification title content

setContentText() //Specify the content of the notification text
setWhen() //Specify the time when the notification was created
setSmallIcon() //Set the small icon for the notification
setLargeIcon() //Set the large icon for the notification

Guess you like

Origin blog.csdn.net/m0_56366502/article/details/128241678