Notification 相关设置以及兼容性

关于Android 的系统通知有时候会收不到的情况,如下给出一些兼容性的设置,代码如下:

  /** 
     * 程序通知,覆盖start
     * 通知栏相关属性设置 */
  @SuppressLint("NewApi")
  private void setNotification() { NotificationManager notificationManager = (NotificationManager) this .getSystemService(android.content.Context.NOTIFICATION_SERVICE); // 设置通知的事件消息 CharSequence contentTitle = getString("标题"); // 通知栏标题 CharSequence contentText = getString("内容"); // 通知栏内容 Intent notificationIntent = new Intent(); notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER); notificationIntent.setClass(this, MainActivity.class); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); PendingIntent contentItent = PendingIntent.getActivity(this, 0, notificationIntent, 0); //以下针对不同版本的兼容设置 if (android.os.Build.VERSION.SDK_INT > 11) { //notification.builder Builder builder = new Notification.Builder(this); builder.setContentIntent(contentItent).setSmallIcon(R.drawable.ic_noti) //设置状态栏里面的图标(小图标)                        .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_notification))//下拉列表里面的图标(大图标)           .setTicker(contentText) //设置状态栏的显示的信息   .setWhen(System.currentTimeMillis()) //设置时间发生时间   .setAutoCancel(false) //设置可以清除   .setOngoing(true)   .setContentTitle(contentTitle) //设置下拉列表里的标题   .setContentText(contentText); //设置上下文内容 Notification notification = builder.getNotification(); notification.flags |= Notification.FLAG_ONGOING_EVENT; //将此通知放到通知栏的"Ongoing"即"正在运行"组中 notification.flags |= Notification.FLAG_NO_CLEAR; //表明在点击了通知栏中的"清除通知"后,此通知不清除,经常与FLAG_ONGOING_EVENT一起使用 notification.defaults = Notification.DEFAULT_LIGHTS; notificationManager.notify(0, notification); }else { Notification notification = new Notification(
         R.drawable.ic_notification, getString(R.string.app_name),   System.currentTimeMillis()); notification.flags
|= Notification.FLAG_ONGOING_EVENT; // 将此通知放到通知栏的"Ongoing"即"正在运行"组中 notification.flags |= Notification.FLAG_NO_CLEAR; // 表明在点击了通知栏中的"清除通知"后,此通知不清除,经常与FLAG_ONGOING_EVENT一起使用 notification.defaults = Notification.DEFAULT_LIGHTS; notification.setLatestEventInfo(this, contentTitle, contentText, contentItent); // 把Notification传递给NotificationManager notificationManager.notify(0, notification); } }

 

posted @ 2014-09-29 16:16  sangxb  阅读(1207)  评论(0编辑  收藏  举报