Android- Notification消息推送和自定义消息推送
2017-02-15 11:22
393 查看
我们的手机,每天只要数据开了,一些应用都会有每天的及时推送,告诉我们最新的消息,提醒我们版本的更新,那么这个技术点就是使用了通知机制的通知栏框架,它使用于交互事件的通知,它是位于顶层可以展开的通知列表Notification有哪些功能作用呢1》显示接收的短信,消息(QQ,微信,新浪,爱奇艺等)2》显示客户端的推送消息3》显示正在进行的事务(正在播放的音乐,下载进度条)通知状态栏主要涉及到两个类,Notification、NotificationManagerNotification是通知消息类,它里面对应的通知栏的各种属性NotificationManager是状态栏通知的管理类,负责发送,清除消息通知等Notification使用步骤<1>.得到通知管理者,因为NotificationManager是一个系统Service,所以需要通过getService得到
NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);<2>.创建Notification
NotificationCompat.Builder builder=new NotificationCompat.Builder(this);<3>.为Notification设置各种属性<4>.通过通知管理者NotificationManager发送通知
notificationManager.notify(0x101,notification);<5>.删除通知notificationManager.cancel(NOTIFICATION_ID);然后再看看完整的代码布局<Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="sendNotification"android:text="发送通知"/>Activitypublic void sendNotification(View view){//实例化通知管理器NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);//实例化通知NotificationCompat.Builder builder=new NotificationCompat.Builder(this);builder.setContentTitle("大事件");//设置通知标题builder.setContentText("不要放孔明灯,容易起火");//设置通知内容builder.setDefaults(NotificationCompat.DEFAULT_ALL);//设置通知的方式,震动、LED灯、音乐等builder.setAutoCancel(true);//点击通知后,状态栏自动删除通知builder.setSmallIcon(android.R.drawable.ic_media_play);//设置小图标builder.setContentIntent(PendingIntent.getActivity(this,0x102,new Intent(this,RaingRecived.class),0));//设置点击通知后将要启动的程序组件对应的PendingIntentNotification notification=builder.build();//发送通知notificationManager.notify(0x101,notification);}如果使用手机的震动,闪光灯的话就需要添加权限<1>.震动权限:<uses-permission android name="android.permission.VIBRATE"><2>.闪光灯权限:<uses-permission android name="android.permission.FLASHLIGHT">如果是自定义不带按钮的通知栏实现方法同上,只是再实例化通知后加上两句代码:
RemoteViews remoteViews=new RemoteViews("com.aa.familyapp",R.layout.activity_mynotification); builder.setCustomContentView(remoteViews);相当于解析了一个布局文件
相关文章推荐
- Android自定义消息推送
- Android 极光推送获取自定义消息
- android入门(八) 状态栏通知(消息推送) Notification
- Android (Notification)消息推送机制
- Android 自定义锁屏 监听系统消息推送
- Android之极光推送发送自定义消息
- Android 自定义Notification消息通知样式并实现与Activity简单交互
- Android (Notification)消息推送机制
- android 接受消息广播,简单的自定义notification样式弹出
- android之notification消息推送机制
- Android 使用极光推送自定义消息打造个性的消息推送效果
- android开发 -- Notification 状态栏 消息推送
- Android之极光推送发送自定义消息
- Android极光推送自定义消息
- Android 使用极光推送消息详细介绍之自定义消息
- Android开发笔记之notification消息推送 通知栏的实现
- Android 主页面顶部栏的通知Notification ,可以自定义通知消息栏的风格,并且点击通知栏进人本程序。
- Android 消息推送(notification) 标题栏icon不显示内容
- Android 使用极光推送自定义消息推送效果
- Android (Notification)消息推送机制