/// <summary> /// 设置扩展 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public bool SetExtraField(string key, string value) { JObject payloadJson = null; JObject extraJson = null; if (RootJson.ContainsKey("payload")) { payloadJson = RootJson.Value <JObject>("payload"); } else { payloadJson = new JObject(); RootJson.Add("payload", payloadJson); } if (payloadJson.ContainsKey("extra")) { extraJson = payloadJson.Value <JObject>("extra"); } else { extraJson = new JObject(); payloadJson.Add("extra", extraJson); } extraJson.Add(key, value); return(true); }
/// <summary> /// Set extra key/value for Android notification /// 设置附加的参数 key value列表 可选 用户自定义key-value。只对"通知" /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public bool SetExtraField(string key, string value) { JObject payloadJson = null; JObject extraJson = null; if (RootJson.Properties().Any(p => p.Name == "payload")) { payloadJson = RootJson.Property("payload").Value.ToObject <JObject>(); } else { payloadJson = new JObject(); RootJson.Add("payload", payloadJson); } if (payloadJson.Properties().Any(p => p.Name == "extra")) { extraJson = payloadJson.Property("extra").Value.ToObject <JObject>(); } else { extraJson = new JObject(); payloadJson.Add("extra", extraJson); } extraJson.Add(key, value); //需要重新赋值,否则值设置不上 payloadJson.Property("extra").Value = extraJson; RootJson.Property("payload").Value = payloadJson; return(true); }
/// <summary> /// 设置预定义的值 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public override bool SetPredefinedKeyValue(string key, object value) { var val = JToken.FromObject(value); if (ROOT_KEYS.Contains(key)) { RootJson.Add(key, val); } else if (APS_KEYS.Contains(key)) { JObject apsJson = null; JObject payloadJson = null; if (RootJson.ContainsKey("payload")) { payloadJson = RootJson.Value <JObject>("payload"); } else { payloadJson = new JObject(); RootJson.Add("payload", payloadJson); } if (payloadJson.ContainsKey("aps")) { apsJson = payloadJson.Value <JObject>("aps"); } else { apsJson = new JObject(); payloadJson.Add("aps", apsJson); } apsJson.Add(key, val); } else if (POLICY_KEYS.Contains(key)) { JObject policyJson = null; if (RootJson.ContainsKey("policy")) { policyJson = RootJson.Value <JObject>("policy"); } else { policyJson = new JObject(); RootJson.Add("policy", policyJson); } policyJson.Add(key, val); } else { if (key == "payload" || key == "aps" || key == "policy") { throw new Exception(" 未找到 " + key + "在集合中"); } else { throw new Exception("未知 key: " + key); } } return(true); }
/// <summary> /// /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public bool SetCustomizedField(string key, string value) { JObject payloadJson = null; if (RootJson.ContainsKey("payload")) { payloadJson = RootJson.Value <JObject>("payload"); } else { payloadJson = new JObject(); RootJson.Add("payload", payloadJson); } payloadJson.Add(key, value); return(true); }
/// <summary> /// Set customized key/value for IOS notification /// 设置自定义的key/value参数到提交的json字符串中 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public bool SetCustomizedField(string key, string value) { //RootJson.put(key, value); JObject payloadJson = null; if (RootJson.Properties().Any(p => p.Name == "payload")) { payloadJson = RootJson.Property("payload").Value.ToObject <JObject>(); } else { payloadJson = new JObject(); RootJson.Add("payload", payloadJson); } payloadJson.Add(key, value); //需要重新赋值,否则值设置不上 RootJson.Property("payload").Value = payloadJson; return(true); }
/// <summary> /// 设置参数到提交json中去 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public sealed override bool SetPredefinedKeyValue(string key, object value) { if (RootKeys.Contains(key)) { // This key should be in the root level RootJson.Add(key, JToken.FromObject(value)); } else if (ApsKeys.Contains(key)) { // This key should be in the aps level JObject apsJson = null; JObject payloadJson = null; if (RootJson.Properties().Any(p => p.Name == "payload")) { payloadJson = RootJson.Property("payload").Value.ToObject <JObject>(); } else { payloadJson = new JObject(); RootJson.Add("payload", payloadJson); } if (payloadJson.Properties().Any(p => p.Name == "aps")) { apsJson = payloadJson.Property("aps").Value.ToObject <JObject>(); } else { apsJson = new JObject(); payloadJson.Add("aps", apsJson); } apsJson.Add(key, JToken.FromObject(value)); //需要重新赋值,否则值设置不上 payloadJson.Property("aps").Value = apsJson; RootJson.Property("payload").Value = payloadJson; } else if (PolicyKeys.Contains(key)) { // This key should be in the body level JObject policyJson = null; if (RootJson.Properties().Any(p => p.Name == "policy")) { policyJson = RootJson.Property("policy").Value.ToObject <JObject>(); } else { policyJson = new JObject(); RootJson.Add("policy", policyJson); } policyJson.Add(key, JToken.FromObject(value)); //需要重新赋值,否则值设置不上 RootJson.Property("policy").Value = policyJson; } else { if (key == "payload" || key == "aps" || key == "policy") { throw new Exception("You don't need to set value for " + key + " , just set values for the sub keys in it."); } else { throw new Exception("Unknownd key: " + key); } } return(true); }
/// <summary> /// 设置预定义的值 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public override bool SetPredefinedKeyValue(string key, object value) { var val = JToken.FromObject(value); if (ROOT_KEYS.Contains(key)) { RootJson.Add(key, val); } else if (PAYLOAD_KEYS.Contains(key)) { JObject payloadJson = null; if (RootJson.ContainsKey("payload")) { payloadJson = RootJson.Value <JObject>("payload"); } else { payloadJson = new JObject(); RootJson.Add("payload", payloadJson); } payloadJson.Add(key, val); } else if (BODY_KEYS.Contains(key)) { JObject bodyJson = null; JObject payloadJson = null; // if (RootJson.ContainsKey("payload")) { payloadJson = RootJson.Value <JObject>("payload"); } else { payloadJson = new JObject(); RootJson.Add("payload", payloadJson); } if (payloadJson.ContainsKey("body")) { bodyJson = payloadJson.Value <JObject>("body"); } else { bodyJson = new JObject(); payloadJson.Add("body", bodyJson); } bodyJson.Add(key, val); } else if (POLICY_KEYS.Contains(key)) { JObject policyJson = null; if (RootJson.ContainsKey("policy")) { policyJson = RootJson.Value <JObject>("policy"); } else { policyJson = new JObject(); RootJson.Add("policy", policyJson); } policyJson.Add(key, val); } else { if (key == "payload" || key == "body" || key == "policy" || key == "extra") { throw new Exception("没有添加 " + key + " , 到HashSet集合"); } else { throw new Exception("未知的 key: " + key); } } return(true); }
/// <summary> /// Set key/value in the RootJson, for the keys can be set please see RootKeys, PayloadKeys, /// BodyKeys and PolicyKeys. /// 设置预定义的key/value /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public sealed override bool SetPredefinedKeyValue(string key, object value) { if (RootKeys.Contains(key)) { // This key should be in the root level RootJson.Add(key, JToken.FromObject(value)); } else if (PayloadKeys.Contains(key)) { // This key should be in the payload level JObject payloadJson = null; if (RootJson.Properties().Any(p => p.Name == "payload")) { payloadJson = RootJson.GetValue("payload").ToObject <JObject>(); } else { payloadJson = new JObject(); RootJson.Add("payload", payloadJson); } payloadJson.Add(key, JToken.FromObject(value)); //需要重新赋值,否则值设置不上 RootJson.Property("payload").Value = payloadJson; } else if (BodyKeys.Contains(key)) { // This key should be in the body level JObject bodyJson = null; JObject payloadJson = null; // 'body' is under 'payload', so build a payload if it doesn't exist if (RootJson.Properties().Any(p => p.Name == "payload")) { payloadJson = RootJson.Property("payload").Value.ToObject <JObject>(); } else { payloadJson = new JObject(); RootJson.Add("payload", payloadJson); } // Get body JSONObject, generate one if not existed if (payloadJson.Properties().Any(p => p.Name == "body")) { bodyJson = payloadJson.GetValue("body").ToObject <JObject>(); } else { bodyJson = new JObject(); payloadJson.Add("body", bodyJson); } bodyJson.Add(key, JToken.FromObject(value)); //需要重新赋值,否则值设置不上 payloadJson.Property("body").Value = bodyJson; RootJson.Property("payload").Value = payloadJson; } else if (PolicyKeys.Contains(key)) { // This key should be in the body level JObject policyJson = null; if (RootJson.Properties().Any(p => p.Name == "policy")) { policyJson = RootJson.Property("policy").Value.ToObject <JObject>(); } else { policyJson = new JObject(); RootJson.Add("policy", policyJson); } policyJson.Add(key, JToken.FromObject(value)); //需要重新赋值,否则值设置不上 RootJson.Property("policy").Value = policyJson; } else { if (key == "payload" || key == "body" || key == "policy" || key == "extra") { throw new Exception("You don't need to set value for " + key + " , just set values for the sub keys in it."); } else { throw new Exception("Unknown key: " + key); } } return(true); }