Android开发——Notification 通知

        最近在看消息推送,从服务端向客户端推送消息。虽然现在有C2DM,AndroidPN,还有第三方的推送平台,但是使用别人的总不如自己来搭建平台。在研究啦,希望有研究过的前辈可以指点下。

       

        消息推送必然要用到Notification。现总结如下:

        要实现手机状态栏的通知,必然要使用两个类:NotificationManager和Notification。

        NotificationManager:状态栏通知的管理类,负责发送、清楚通知等,它是一个系统Service,必须通过getSystemService()方法来获取。 

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

        NotificationManager常用方法:

        public void cancelAll() 移除所有通知(只是针对当前Context下的Notification)

        public  void cancel(int id) 移除标记为id的通知 (只是针对当前Context下的所有Notification)

        public  void notify(String tag ,int id, Notification notification) 将通知加入状态栏,标签为tag,标记为id

        public  void notify(int id, Notification notification) 将通知加入状态栏,标记为id

        Notification:具体的状态栏通知对象,可以设置icon、文字、提示声音、振动等等参数。

// 创建一个Notification  
Notification notification = new Notification();  

        Notification的一些参数:

       

        DEFAULT_ALL    使用所有默认值,比如声音,震动,闪屏等等

        DEFAULT_LIGHTS 使用默认闪光提示

        DEFAULT_SOUNDS 使用默认提示声音

        DEFAULT_VIBRATE 使用默认手机震动

【说明】:加入手机震动,一定要在manifest.xml中加入权限:

<uses-permission android:name="android.permission.VIBRATE" />

 // 设置显示在手机最上边的状态栏的图标  
 notification.icon = R.drawable.notification;  
 // 当notification被放到状态栏上的时候,会有一个提示内容  
 notification.tickerText = "注意了,我被扔到状态栏了"; 
 // 添加声音提示  
 notification.defaults=Notification.DEFAULT_SOUND;
// audioStreamType的值必须AudioManager中的值,代表着响铃的模式
// audioStreamType 当声音响起时,所用的音频流的类型  
 notification.audioStreamType= android.media.AudioManager.ADJUST_LOWER;  
 //定义自己的声音提示
 notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");//使用sd卡的铃声
   //使用系统自带的铃声
 notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
 //声音循环播放,知道用户响应为止
 notification.flags|=notification.FLAG_INSISTENT;
 //使用默认的闪光
 notification.defaults|=Notification.DEFAULT_LIGHTS; 
 //使用默认的震动
 notification.defaults|=Notification.DEFAULT_VIBRATE; 

        以上是通知显示在状态栏上的一些属性设置内容。当用户下拉状态栏,并且点击通知后,我们要显示通知的具体内容。

Intent intent = new Intent(this, NotificationDetailActivity.class);  
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG                             _ONE_SHOT);  
 // 下拉状态栏,出现消息的提示内容, 
notification.setLatestEventInfo(this, "内容提示:", "我就是一个测试文件", pendingIntent); 

 

 

        当点击通知提示之后,跳转到通知具体内容的activity:NotificationDetailActivity。

        notification.setLatestEventInfo的参数如下:

        第二个参数:下拉状态栏时显示的消息标题

        第三个参数:下拉状态栏时显示的消息内容

        第四个参数:点击该通知时执行页面跳转      

        

// 把Notification传递给NotificationManager  
notificationManager.notify(1, notification);

        参数设置完成后,NotificationManager将通知加入状态栏。NotificationManager里的notify(id,notification)中的id是用来唯一标识我们当前的这个notification的标识符通过cancel方法删除通知时,传递的就是这个值。

 

    //删除通知 
    private void clearNotification(){
        // 启动后删除之前我们定义的通知  
     NotificationManager notificationManager = (NotificationManager) this
                .getSystemService(NOTIFICATION_SERVICE);  
        notificationManager.cancel(1); 
    }

 

猜你喜欢

转载自eleanor-fan.iteye.com/blog/2068754