一、说明


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);
 
 
 
 

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

 
 
 
 
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);