普通通知
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);
}
}
通知分组
获取通知管理器
NotificationManager mNotifacationManager = getSystemService(NOTIFICATION_SERVICE);
创建多个通知
NotificationCompat.Builder mBuilder1 = new NotificationCompat.Builder(this);
NotificationCompat.Builder mBuilder2 = new NotificationCompat.Builder(this);
设置分组
mBuilder1.setWhen(System.currentTimeMillis())
.setContentTitle("first content title")
.setContentText("first content text")
.setSmallIcon(R.mipmap.ic_launcher)
.setGroup("a");
mBuilder1.setWhen(System.currentTimeMillis())
...
.setGroup("b");
发送通知
mNotifacationManager.notify(1, mBuilder1.build());
mNotifacationManager.notify(2, mBuilder2.build());
进度条通知
获取通知管理器
NotificationManager nm= getSystemService(NOTIFICATION_SERVICE);
创建通知
NotificationCompat.Builder builder= new NotificationCompat.Builder(this);
builder.setContentText("安装中...");
builder.setProgress(0,0,true);
开启线程更新进度
new Thread(new Runnable() {
public void run() {
for(int i=0;i<100;i++){
builder.setProgress(100,i,false);
nm.notify(3,builder.build());
}
自定义通知
private void userNotification() {
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentText("内容");
builder.setContentTitle("头部");
/**
* RemoteViews是可以在别的进程(系统进程)中显示的View,并且提供了一组跨进程更新它界面的操作
* 两个参数,第一个布局所在包名
* 第二个是布局Id
* 布局文件是自己创建的,随便一个线性布局,加一个textView和ImageView即可
*/
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.simple_layout);
private void notificationStyle() {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("列表通知");
// builder.setContentTitle("大图通知");
//通知内容为大图片
Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();
bigPictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
//通知内容为列表显示
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());
}