Notification消息栏通知的实现

消息栏通知的实现:

方法1:直接new Notification()

NotificationManager manager=(NotificationManager)this.getSystemService(this.NOTIFICATION_SERVICE);

Notification notification = newNotification(icon, tickerText, when);//参数是消息栏通知的图片,弹通知时的标题,显示的时间

notification.flags |=Notification.FLAG_AUTO_CANCEL; // 在通知栏上点击此通知后自动清除此通知

PendingIntent contentIntent = PendingIntent.getActivity(context,0,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);

notification.sound = Uri
      .parse("android.resource://cn.otrue.patienthealthmanager/"
            + R.raw.tips);//使用自定义的铃声

notification.setLatestEventInfo(context,contentTitle, contentText,contentIntent);

int n=0;//可以为任意数字

mNotificationManager.notify(n, notification);

 

效果的实现

二、有效果的通知(震动、声音、亮灯)

这些功能的实现都是在通知之前进行设置

(1)震动

 notification.defaults |=Notification.DEFAULT_VIBRATE;

 还需要添加使用权限:<uses-permissionandroid:name="android.permission.VIBRATE"></uses-permission>

(2)声音

notification.defaults|=Notification.DEFAULT_VIBRATE;

(3)亮灯

 

三、显示效果---点击通知栏

(1)点击后消失

notification.flags = Notification.FLAG_AUTO_CANCEL;

(2)点击后无法销毁

notification.flags =Notification.FLAG_NO_CLEAR;

 

 

 

方法2:通过builder获取notification

NotificationManagermanager=(NotificationManager) this.getSystemService(this.NOTIFICATION_SERVICE);

NotificationCompat.Builder builder1 = newNotificationCompat.Builder(MainActivity.this);

//也可以是Notification.Builderbuilder1 = new Notification.Builder(MainActivity.this);

builder1.setSmallIcon(R.drawable.ic_launcher);//设置图标

builder1.setTicker("显示第二个通知");//消息栏弹出来时显示的内容

builder1.setContentTitle("通知"); //在下拉通知栏中查看通知时,显示的通知的标题

builder1.setContentText(content.getText().toString());//在下拉植栏中查看通知时,显示的通知的内容

builder1.setWhen(System.currentTimeMillis());//发送时间

builder1.setDefaults(Notification.DEFAULT_ALL);//设置默认的提示音,振动方式,灯光

builder1.setAutoCancel(true);//点击后是否消失

Intent intent =new Intent(MainActivity.this,TwoActivity.class);

PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this,Integer.parseInt(num.getText().toString()), intent, flags);

builder1.setContentIntent(pendingIntent);

Notification notification1 =builder1.build();

manager.notify(124, notification1); // 通过通知管理器发送通知

 

 

自定义布局的消息栏通知

 

NotificationManagermanager=(NotificationManager) this.getSystemService(this.NOTIFICATION_SERVICE);

                   NotificationCompat.Builderbuilder1 = new NotificationCompat.Builder(MainActivity.this);

builder1.setSmallIcon(R.drawable.ic_launcher);//设置图标

builder1.setTicker("显示第二个通知");//消息栏弹出来时显示的内容

builder1.setContentTitle("通知"); //在下拉通知栏中查看通知时,显示的通知的标题

builder1.setContentText(content.getText().toString());//在下拉植栏中查看通知时,显示的通知的内容

builder1.setWhen(System.currentTimeMillis());//发送时间

builder1.setDefaults(Notification.DEFAULT_ALL);//设置默认的提示音,振动方式,灯光

builder1.setAutoCancel(true);//点击后是否消失

RemoteViews view = newRemoteViews(getPackageName(), R.layout.button_play);

 

PendingIntent intent1 =PendingIntent.getActivity(this, Integer.parseInt(num.getText().toString()), newIntent(this,ThreeActivity.class).putExtra("send", "开始"), 0);//自定义布局的意图的flag不能相同,相同则只会以最新的为标准//设置未知意图监听

view.setOnClickPendingIntent(R.id.play,intent1);//设置自定义布局上的按钮的未知意图的点击事件

                  

PendingIntent intent2 =PendingIntent.getActivity(this, Integer.parseInt(num.getText().toString()), newIntent(this,ThreeActivity.class).putExtra("send", "停止"), 1);//自定义布局的意图的flag不能相同,相同则只会以最新的为标准                       

view.setOnClickPendingIntent(R.id.stop,intent2);//设置位置意图上的自定义布局上的控件的监听

Intent intent =new Intent(MainActivity.this,TwoActivity.class);

PendingIntent pendingIntent=null;

pendingIntent=PendingIntent.getActivity(MainActivity.this,Integer.parseInt(num.getText().toString()), intent, flags);

builder1.setContentIntent(pendingIntent);

Notification notification1 =builder1.build();

notification1.contentView = view;

manager.notify(Integer.parseInt(num.getText().toString()),notification1); // 通过通知管理器发送通知
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值