您的位置:首页 > 移动开发 > Android开发

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

2016-01-07 17:23 579 查看
创建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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: