//普通的消息通知
NotificationManager manager = (NotificationManager) this.getSystemService(this.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(this);//新建Notification.Builder对象
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, TestActivity.class), 0);
builder.setContentTitle("service test");
builder.setContentText("message");
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentIntent(pendingIntent);
Notification notification = builder.getNotification();//将builder对象转换为普通的notification
notification.flags |= Notification.FLAG_AUTO_CANCEL;//点击通知后通知消失
manager.notify(1, notification);//运行notification
startForeground(1, notification);
//自定义布局带大图的消息通知
NotificationManager systemService = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification);
remoteViews.setImageViewResource(R.id.iv_imageview,R.mipmap.programmer);
remoteViews.setTextViewText(R.id.tv_notification,"我是程序员。。。。。。");
remoteViews.setImageViewResource(R.id.iv_logo,R.mipmap.ic_launcher);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setOngoing(false);
builder.setAutoCancel(false);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("主题名称");
builder.setContentText("测试测试测试");
Notification notification = builder.build();
notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS;
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.when = System.currentTimeMillis();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notification.bigContentView = remoteViews;
}
Intent i = new Intent(this,TestActivity.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, i, PendingIntent.FLAG_UPDATE_CURRENT);
notification.contentIntent = pendingIntent;
systemService.notify(0, notification);
startForeground(1, notification);
基本属性
public Builder setTicker(CharSequence tickerText)
设置状态栏开始动画的文字public Builder setContentTitle(CharSequence title)
设置内容区的标题,必须设置public Builder setContentText(CharSequence text)
设置内容区的内容,必须设置public Builder setContentIntent(PendingIntent intent)
设置点击通知后操作(可以跳转Activity,打开Service,或者发送广播)public Builder setColor(@ColorInt int argb)
这个可以设置smallIcon的背景色public Builder setSmallIcon(@DrawableRes int icon)
设置小图标,必须设置public Builder setLargeIcon(Bitmap b)
设置打开通知栏后的大图标public Builder setWhen(long when)
设置显示通知的时间,不设置默认获取系统时间,这个值会在Notification上面显示出来public Builder setAutoCancel(boolean autoCancel)
设置为true,点击该条通知会自动删除,false时只能通过滑动来删除public Builder setPriority(int pri)
设置优先级,级别高的排在前面public Builder setDefaults(int defaults)
设置上述铃声,振动,闪烁用|分隔,常量在Notification里public Builder setOngoing(boolean ongoing)
设置是否为一个正在进行中的通知,这一类型的通知将无法删除
通知的提醒方式
- 声音提醒
默认声音
notification.defaults |= Notification.DEFAULT_SOUND;
自定义声音
notification.sound = Uri.parse("file:///sdcard0/notification.ogg"); - 震动提醒
默认振动
notification.defaults |= Notification.DEFAULT_VIBRATE;
自定义振动
long[] vibrate = {100, 200, 300, 400}; //震动效果,表示在100、200、300、400这些时间点交替启动和关闭震动
notification.vibrate = vibrate; - 闪烁提醒
默认闪烁
notification.defaults |= Notification.DEFAULT_LIGHTS;
自定义闪烁
notification.ledARGB = 0xff00ff00; // LED灯的颜色,绿灯
notification.ledOnMS = 300; // LED灯显示的毫秒数,300毫秒
notification.ledOffMS = 1000; // LED灯关闭的毫秒数,1000毫秒
notification.flags |= Notification.FLAG_SHOW_LIGHTS; // 必须加上这个标志