Android之Notification(通知)

在使用AndroidAPP的时候,我们想接受一个后台运行的APP的通知,手机上方状态栏出现通知的图标,下拉后会显示详细信息,这个功能作用很常见也很有意义,不仅能让用户订阅自己喜欢的通知,还能为APP做一个很好的推荐。

在Android中,通知主要用到了NotificationManager类和Notification类。

//通知管理类来对通知进行管理
 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() //设置通知标题内容

setContentText() //指定通知正文的内容
setWhen() //指定通知被创建的时间
setSmallIcon()  //设置通知的小图标
setLargeIcon()  //设置通知的大图标

猜你喜欢

转载自blog.csdn.net/m0_56366502/article/details/128241678