基于百度推送android notification的使用之合并通知栏

创建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代码进行的修改
@Override
	public 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 block
				e.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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值