首页 > 代码库 > Android发送多个notification

Android发送多个notification

//Android发送多个notification  ,<span style="font-size: 11.8181819915771px; font-family: Arial, Helvetica, sans-serif;">PendingIntent的ID很重要。</span>
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, <strong><span style="color:#ff0000;">NOTIFICATION_ID</span></strong>, 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 stub
						nm.cancel(NOTIFICATION_ID);
					}
				}, disappearTime);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

Android发送多个notification