创建Notification

public void showmynotification(Context context,int num) {

   
NotificationManager  manager = (NotificationManager)context. getSystemService(Context.NOTIFICATION_SERVICE);
//点击的意图ACTION是跳转到Intent Intent resultIntent = new Intent(this, MainActivity.class); resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);  
 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
   
    // 这里pendingIntent 根据自己的需求设定,也可以是点击通知后发送广播 然后在自己的广播接收器中处理点击事件
   
   //  Intent resultIntent = new Intent(BROAST_ACTION);   // PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);  
 notifyBuilder = new NotificationCompat.Builder(this)           
 /*设置large icon*/            
.setLargeIcon(bitmap)            
 /*设置small icon*/           
 .setSmallIcon(R.drawable.ic_launcher)           
 /*设置title*/            
.setContentTitle("通知")           
 /*设置详细文本*/            
.setContentText("Hello world")            
 /*设置发出通知的时间为发出通知时的系统时间*/           
 .setWhen(System.currentTimeMillis())             
/*设置发出通知时在status bar进行提醒*/           
 .setTicker("来自问月的祝福")            
/*setOngoing(boolean)设为true,notification将无法通过左右滑动的方式清除 * 可用于添加常驻通知,必须调用cancle方法来清除 */            .setOngoing(true)            
 /*设置点击后通知消失*/            
.setAutoCancel(true)             
/*设置通知数量的显示类似于QQ那种,用于同志的合并*/            
.setNumber(num)            
 /*点击跳转到MainActivity*/           
 .setContentIntent(pendingIntent);   
 manager.notify(1, notifyBuilder.build());
}
   
知道了如何创建通知,就能根据自己的需求显示通知栏了。我们项目使用了百度推送api,但是百度推送的消息会一条接着一条的显示,看着特别乱,这就需要对消息进行合并操作
使用百度推送会自定义一个广播接收器,里面有两个核心方法
onNotificationClicked:接收通知点击的函数
onNotificationArrived:接收通知到达的函数
在广播接收器中定义变量
private static int num = 0; //记录消息的个数
如下是对百度推送api的demo代码进行的修改
@Overridepublic void onNotificationArrived(Context context, String title,String description, String customContentString) {String notifyString = "onNotificationArrived  title=\"" + title+ "\" description=\"" + description + "\" customContent="+ customContentString;Log.d(TAG, notifyString);num++;if (num > 1) {
                       // 清空原有的通知manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);manager.cancelAll();showMyNotification(context, num);}// 自定义内容获取方式,mykey和myvalue对应通知推送时自定义内容中设置的键和值if (!TextUtils.isEmpty(customContentString)) {JSONObject customJson = null;try {customJson = new JSONObject(customContentString);String myvalue = null;if (!customJson.isNull("mykey")) {myvalue = customJson.getString("mykey");}} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}}// Demo更新界面展示代码,应用请在这里加入自己的处理逻辑// 你可以參考 onNotificationClicked中的提示从自定义内容获取具体值updateContent(context, notifyString);}
这样就能对百度推送的消息进行合并了
效果如下:
   
备注:可以通过RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_NOTIFICATION)获取系统通知铃声的URI。


参考文章:http://www.itnose.net/detail/6169442.html

                    http://www.codeceo.com/article/android-notification-4-types.html

                    http://www.2cto.com/kf/201408/327782.html

更多相关文章

  1. android调用邮件应用发送email
  2. android 的popwindow弹窗
  3. Android:ImageView 设置图片
  4. goolge 地图地址位置解析
  5. Android(安卓)studio中关于 No cached version of **** availabl
  6. android listview 三种适配器设置
  7. Android开发规范
  8. Android(安卓)EditText 密码输入框可见性设置
  9. Android(安卓)8.0和8.1通知栏

随机推荐

  1. OpenCV 下载驿站(百度云盘下载,同步更新)
  2. android 之 广播相关
  3. android加载字体内存泄漏的处理方法
  4. Android开发Android(安卓)studio之gradle
  5. Activity的启动模式和悬浮窗
  6. Android ROM开发
  7. Android动画之帧动画
  8. jenkins搭建及基础配置
  9. Mac os下android源码下载及安装
  10. AndroidStudio给kotlin添加类似logm的快