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();
		}
	}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值