![](https://img-blog.csdnimg.cn/img_convert/eb956e7d8543e3ab9ee6d41e97276a89.png)
这是我写的通知 可能挺欠缺的
下面来讲一下我的理解
NotificationManager 定义一个消息管理器
然后一共是五步
第一步骤是创建消息管理器
第二步是创建构造者
第三部是设置属性
第四部是创建通知
第五步是创建消息管理器
![](https://img-blog.csdnimg.cn/img_convert/53dd16e9d56fddf86dbc98cb458775ed.png)
一个APP可能短时间之内可能会推送很多条消息给用户,占用大量空间,消息都堆在一起,也大大降低了用户的体验,所以使用分组将同类型的消息统一管理。
private void sendNotification() {
//创建构造者
Notification.Builder builder = new Notification.Builder(this);
//设置属性 setSamllIcon该属性必须设置
builder.setSmallIcon(R.mipmap.ic_launcher); //必须设置
builder.setContentTitle("我是标题"); //建议设置
builder.setContentText("我是内容"); //建议设置
// builder.setTicker("我是提示信息");
// builder.setContentInfo("我是附加信息"); //7.0以后已经过期
//创建对象.发送的就是这个对象
Notification build = builder.build();
//获取通知管理器,负责发通知、清除通知等
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//TODO :发送通知
//参数一 id 通知的id(稍后介绍意义) 参数二 通知对象
notificationManager.notify(1,build);
}
}
/通知内容为列表显示
Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
inboxStyle.addLine("李白");
inboxStyle.addLine("猴子");
inboxStyle.addLine("露娜");
builder.setStyle(inboxStyle);
//不能跨APP
Intent intent = new Intent(this, MainActivity.class);
//intent - PendingIntent
PendingIntent intent1 = PendingIntent.getActivity(this, 10, intent, PendingIntent.FLAG_ONE_SHOT);
builder.setFullScreenIntent(intent1, true);
builder.setContentIntent(intent1);
manager.notify(9, builder.build());
}