Chinaunix首页 | 论坛 | 博客
  • 博客访问: 134906
  • 博文数量: 34
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 700
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-12 16:52
文章分类

全部博文(34)

文章存档

2015年(13)

2014年(21)

我的朋友

分类: Android平台

2015-01-28 15:46:41


[java] view plaincopy
  1. //Android发送多个notification  ,PendingIntent的ID很重要。  
[java] view plaincopy
  1. public void addNotification(JSONArray args, CallbackContext callbackContext) throws JSONException {  
  2.         //NOTIFICATION_ID = args.getInt(6);  
  3.         NOTIFICATION_ID = (int)(Math.random()*10000);  
  4.           
  5.         try {  
  6.             nm = (NotificationManager) cordova.getActivity().getSystemService(  
  7.                     Context.NOTIFICATION_SERVICE);  
  8.             Intent intent = new Intent(cx, cordova.getActivity().getClass());  
  9.             intent.putExtra("clickAction", args.getString(4));  
  10.             String clickActionParams = args.getJSONObject(5).toString();  
  11.             intent.putExtra("clickActionParams", clickActionParams);  
  12.             PendingIntent pIntent = PendingIntent.getActivity(cx, "color:#ff0000;">NOTIFICATION_ID, intent,  
  13.                     PendingIntent.FLAG_UPDATE_CURRENT);  
  14.             int version = android.os.Build.VERSION.SDK_INT;  
  15.             Notification notify;  
  16.             // 如果版本号大于15.即采用notification.builder方法,如果版本号小于15,即采用旧方法,避免类似卢峰手机的问题  
  17.             if (version > 15) {  
  18.                 notify = new Notification.Builder(cx)  
  19.                         // 设置打开该通知,该通知自动消失  
  20.                         .setAutoCancel(true)  
  21.                         // 设置显示在状态栏的通知提示信息  
  22.                         .setTicker(args.getString(0))  
  23.                         // 设置通知的图标  
  24.                         .setSmallIcon(R.drawable.icon)  
  25.                         // 设置通知内容的标题  
  26.                         .setContentTitle(args.getString(0))  
  27.                         // 设置通知内容  
  28.                         .setContentText(args.getString(1) + NOTIFICATION_ID)  
  29.                         .setWhen(System.currentTimeMillis())  
  30.                         // 设改通知将要启动程序的Intent  
  31.                         .setContentIntent(pIntent).build();  
  32.             } else {  
  33.                 notify = new Notification(R.drawable.icon, args.getString(0),  
  34.                         System.currentTimeMillis());  
  35.                 notify.setLatestEventInfo(cx, args.getString(0),  
  36.                         args.getString(1), pIntent);  
  37.             }  
  38.   
  39.             // 发送通知  
  40.             nm.notify(NOTIFICATION_ID, notify);  
  41.             // 通知显示X秒后自动清除  
  42.             if (args.getBoolean(2)) {  
  43.                 disappearTime = args.getLong(3);  
  44.                 Handler handler = new Handler();  
  45.                 handler.postDelayed(new Runnable() {  
  46.                     public void run() {  
  47.                         // TODO Auto-generated method stub  
  48.                         nm.cancel(NOTIFICATION_ID);  
  49.                     }  
  50.                 }, disappearTime);  
  51.             }  
  52.         } catch (JSONException e) {  
  53.             e.printStackTrace();  
  54.         } catch (Exception e) {  
  55.             e.printStackTrace();  
  56.         }  
  57.     }  
文章转自
阅读(672) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~