Notification.Builder builder1 = new Notification.Builder(MainActivity.this);
//实例化通知栏构造器Notification.Builder
builder1.setSmallIcon(R.drawable.advise2); //设置图标
builder1.setTicker("显示第二个通知");
//设置通知首次出现在通知栏显示的内容,例如:您有一条短信请查收。
builder1.setContentTitle("通知"); //设置标题
builder1.setContentText("点击查看详细内容");
//设置接收消息后,将状态栏下拉后的消息内容
builder1.setWhen(System.currentTimeMillis()); //发送时间
builder1.setDefaults(Notification.DEFAULT_ALL);
//设置默认的提示音,振动方式,灯光
builder1.setAutoCancel(true);//打开程序后图标消失I
Intent intent =new Intent (MainActivity.this,Center.class);
//设置意图,这里是跳转到一个界面。
PendingIntent pendingIntent =PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
builder1.setContentIntent(pendingIntent);//设置你点击消息后的意图
Notification notification1 = builder1.build();//得到一个 notification
NotificationManager Manager=(NotificationManager)this.
getSystemService(Context.NOTIFICATION_SERVICE)
//获取通知栏管理者
Manager.notify(124, notification1); // 通过通知管理器发送通知
1.实例化通知栏之后通过给他添加.flags属性赋值。
- Notification notification = mBuilder.build();
- notification.flags = Notification.FLAG_AUTO_CANCEL;
2.通过setContentIntent(PendingIntent intent)方法中的意图设置对应的flags
- public PendingIntent getDefalutIntent(int flags){
- PendingIntent pendingIntent= PendingIntent.
- getActivity(this, 1, new Intent(), flags);
- return pendingIntent;
提醒标志符成员:
Notification.FLAG_SHOW_LIGHTS//三色灯提醒,在使用三色灯提醒时候必须加该标志符
Notification.FLAG_ONGOING_EVENT //发起正在运行事件(活动中)
Notification.FLAG_INSISTENT
//让声音、振动无限循环,直到用户响应 (取消或者打开)
Notification.FLAG_ONLY_ALERT_ONCE //发起Notification后,铃声和震动均只执行一次
Notification.FLAG_AUTO_CANCEL //用户单击通知后自动消失
Notification.FLAG_NO_CLEAR //只有全部清除时,Notification才会清除 ,不清楚该通知(QQ的通知无法清除,就是用的这个)
Notification.FLAG_FOREGROUND_SERVICE //表示正在运行的服务
(3)方法 setDefaults(int defaults)
(NotificationCompat.Builder中的方法,用于提示)
Notification.DEFAULT_VIBRATE //添加默认震动提醒 需要 VIBRATE permission
Notification.DEFAULT_SOUND // 添加默认声音提醒
Notification.DEFAULT_LIGHTS// 添加默认三色灯提醒
Notification.DEFAULT_ALL// 添加默认以上3种全部提醒
- 方法:setVibrate(long[] pattern)
- .setVibrate(new long[] {0,300,500,700});
实现效果:延迟0ms,然后振动300ms,在延迟500ms,接着在振动700ms。
学习网站: