Android显示通知栏消息

一、说明
andorid应用经常会有一些推送消息,将数据从后台推向移动端。可能是一些更新,新闻,或者广告推送都会有,看你的APP是什么类型的~~  现在也有很多推送的SDK  友盟推送,腾讯信鸽推送。。。可以去试试。现在来测试一下如何使用通知栏来显示消息。

二、了解常用类和方法。
1. NotificationManager :  是状态栏通知的管理类,负责发通知、清楚通知等。
     NotificationManager 是一个系统Service,必须通过 getSystemService()方法来获取
代码如下:NotificationManager nm= (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
2.Notification:是具体的状态栏通知对象,可以设置icon、文字、提示声音、振动等等参数。

下面是设置一个通知需要的基本参数:
  • An icon  (通知的图标)
  • A title and expanded message  (通知的标题和内容)
  • PendingIntent   (点击通知执行页面跳转)

可选的设置:

  • A ticker-text message (状态栏顶部提示消息)
  • An alert sound    (提示音)
  • A vibrate setting  (振动)
  • A flashing LED setting  (灯光)
  • 等等


3.创建Notification

通过NotificationManager  的 notify(int, Notification) 方法来启动Notification。

   第一个参数唯一的标识该Notification,第二个参数就是Notification对象。


4.更新Notification

调用Notification的 setLatestEventInfo方法来更新内容,然后再调用NotificationManager的notify()方法即可。(具体可以看下面的实例)

 


5.删除Notification

通过NotificationManager  的cancel(int)方法,来清除某个通知。其中参数就是Notification的唯一标识ID。

当然也可以通过  cancelAll() 来清除状态栏所有的通知。

 


6.Notification设置(振动、铃声等)

三、创建一个案例

默认的通知可以设置要显示的标题,内容图片
Notification  baseNF = new Notification.Builder(MainActivity. this ) //设置启动的context
       
.setContentTitle( "Title01" ) //设置标题
       
.setContentText( "Content01" ) //设置内容
       
.setSmallIcon(R.mipmap. ic_launcher ) //设置要显示的两个图片 小图片可以设置资源文件,大图片为bitmap类型所以需要decodeResource
       
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap. ic_launcher ))
        .build();
其他常用可选设置:
//通知的默认参数 DEFAULT_SOUND, DEFAULT_VIBRATE, DEFAULT_LIGHTS.
//如果要全部采用默认值, 用 DEFAULT_ALL.
//此处采用默认声音
baseNF. defaults |= Notification. DEFAULT_SOUND ;
baseNF.
defaults |= Notification. DEFAULT_VIBRATE ;
baseNF.
defaults |= Notification. DEFAULT_LIGHTS ;

//让声音、振动无限循环,直到用户响应
baseNF. flags |= Notification. FLAG_INSISTENT ;

//通知被点击后,自动消失
baseNF. flags |= Notification. FLAG_AUTO_CANCEL ;

//点击'Clear'时,不清楚该通知(QQ的通知无法清除,就是用的这个)
baseNF. flags |= Notification. FLAG_NO_CLEAR ;

最后执行
//发出状态栏通知
nm .notify( Notification_ID_BASE , baseNF);

成功发送一条通知。= =有各种丧心病狂的设置。

赶紧设置个按钮执行cancel取消掉它。 Notification_ID_BASE 相当于一个标记可以用来取消掉这个通知
nm .cancel( Notification_ID_BASE );

可以用自己的声音和自己设置震动的形式
//第一个参数: 振动前等待的时间
//第二个参数: 第一次振动的时长、以此类推
long [] vir = { 0 , 100 , 200 , 300 };
baseNF.vibrate= vir;
//自定义声音
baseNF. sound = Uri.withAppendedPath(MediaStore.Audio.Media. INTERNAL_CONTENT_URI , "Spring" );
//清除所有由这个对象发送出的通知
nm.cancelAll();

可以自定义布局来展示要显示的东西

布局设置
<? xml version= "1.0" encoding= "utf-8" ?>
<
LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
   
android :orientation= "horizontal"
   
android :layout_width= "fill_parent"
   
android :layout_height= "fill_parent"
   
android :padding= "3dp"
   
>
    <
ImageView android :id= "@+id/image"
       
android :layout_width= "wrap_content"
       
android :layout_height= "fill_parent"
       
android :layout_marginRight= "10dp"
       
/>
    <
TextView android :id= "@+id/text"
       
android :layout_width= "wrap_content"
       
android :layout_height= "fill_parent"
       
android :textColor= "#000"
       
/>
</LinearLayout
//自定义下拉视图,比如下载软件时,显示的进度条。
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout. custom );
contentView.setImageViewResource(R.id.
image , R.mipmap. ic_launcher );
contentView.setTextViewText(R.id.
text , "Hello, this message is in a custom expanded view" );
baseNF.
contentView = contentView;

//使用自定义下拉视图时 必须定义 contentIntent
baseNF. contentIntent = pd ;
nm .notify(3 , baseNF);
深入学习可看这里
:http://blog.csdn.net/vipzjyno1/article/details/25248021
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值