Android發送多個notification
//Android發送多個notification ,PendingIntent的ID很重要。
public void addNotification(JSONArray args, CallbackContext callbackContext) throws JSONException {//NOTIFICATION_ID = args.getInt(6);NOTIFICATION_ID = (int)(Math.random()*10000);try {nm = (NotificationManager) cordova.getActivity().getSystemService(Context.NOTIFICATION_SERVICE);Intent intent = new Intent(cx, cordova.getActivity().getClass());intent.putExtra("clickAction", args.getString(4));String clickActionParams = args.getJSONObject(5).toString();intent.putExtra("clickActionParams", clickActionParams);PendingIntent pIntent = PendingIntent.getActivity(cx, NOTIFICATION_ID, intent,PendingIntent.FLAG_UPDATE_CURRENT);int version = android.os.Build.VERSION.SDK_INT;Notification notify;// 如果版本號碼大於15.即採用notification.builder方法,如果版本號碼小於15,即採用舊方法,避免類似盧峰手機的問題if (version > 15) {notify = new Notification.Builder(cx)// 設定開啟該通知,該通知自動消失.setAutoCancel(true)// 設定顯示在狀態列的通知提示資訊.setTicker(args.getString(0))// 設定通知的表徵圖.setSmallIcon(R.drawable.icon)// 設定通知內容的標題.setContentTitle(args.getString(0))// 設定通知內容.setContentText(args.getString(1) + NOTIFICATION_ID).setWhen(System.currentTimeMillis())// 設改通知將要啟動程式的Intent.setContentIntent(pIntent).build();} else {notify = new Notification(R.drawable.icon, args.getString(0),System.currentTimeMillis());notify.setLatestEventInfo(cx, args.getString(0),args.getString(1), pIntent);}// 發送通知nm.notify(NOTIFICATION_ID, notify);// 通知顯示X秒後自動清除if (args.getBoolean(2)) {disappearTime = args.getLong(3);Handler handler = new Handler();handler.postDelayed(new Runnable() {public void run() {// TODO Auto-generated method stubnm.cancel(NOTIFICATION_ID);}}, disappearTime);}} catch (JSONException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}}