创建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 8.0和8.1通知栏
  2. android TextView 如何实现消息滚动
  3. android 自定义通知消息设置背景色不生效,导致部分机型显示白色字
  4. Android Handler消息机制(源码分析)
  5. Android——自定义通知栏使用
  6. Android的消息机制的总体流程

随机推荐

  1. 分享一些流畅的适合开发的 Android(安卓)
  2. windows系统上安装配置使用Android(安卓)
  3. Android中的AIDL学习笔记(一)
  4. 再论android 2.2数据连接过程
  5. Android(安卓)进阶——Android(安卓)Stud
  6. 如何找到最好的Android应用程序开发者为
  7. 关于android的mk文件的一些见解
  8. Android(安卓)sdk manager无法更新问题,使
  9. Android(安卓)图片压缩终极解决方案
  10. Unity与Android对比学习之生命周期方法