基于百度推送android notification的使用之合并通知栏
来源:互联网 发布:surge for mac 编辑:程序博客网 时间:2024/05/06 15:55
创建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
0 0
- 基于百度推送android notification的使用之合并通知栏
- android通知栏之Notification的使用
- Android-Notification推送栏通知
- Android 使用Notification创建推送通知
- Android之手机通知栏Notification的使用(二)
- Android开发笔记之notification消息推送 通知栏的实现
- android中通知栏notification的使用
- android通知栏(Notification)的使用
- Android--Notification通知栏的使用
- android通知(Notification)的使用
- android Notification(通知) 的使用
- Android通知Notification的使用
- ANDROID NOTIFICATION 通知的使用
- android整合--notification推送通知
- android整合--notification推送通知
- 【Android】使用Notification实现状态通知栏的通知
- Android 之 Notification通知栏
- android Notification 通知栏使用
- JVM原理
- ViewPager和SlidingPaneLayout滑动冲突
- 查看 cpu 过高 分析线程状态
- 兄弟会兄弟会教育学习
- Android主流网络请求框架Volley
- 基于百度推送android notification的使用之合并通知栏
- 玩转树莓派--文件操作
- Android—慎用注解框架butterknife-7.0.1
- TensorFlow 中文文档
- 倒排索引 -- 深入搜索引擎的工作原理 Inverted Indexes – Inside How Search Engines Work
- 方向梯度直方图(HOG,Histogram of Gradient)
- python自动化测试常见问题
- Word2Vec 问题思考
- VS2010+ Ankhsvn使用详解