/// <summary>
/// 发送云吧信息
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
[HttpGet]
public HttpResponseMessage SendMessage(string member_id,string member_platform,string title)
{
HttpResponseMessage result;
dynamic o = new { exist = false };
bool isExist = false;
try
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
string timestamp = Convert.ToInt64(ts.TotalSeconds).ToString();
string appkey = string.Empty;
string app_master_secret = string.Empty;
var param = new object();
if (member_platform == "android")
{
appkey = "appkey";
app_master_secret = "app_master_secret";
param = new
{
appkey = appkey,
timestamp = timestamp,
type = "customizedcast",
alias_type = "UserGroup",
alias = member_id,
payload = new
{
display_type = "notification", //消息类型:消息
body = new
{
ticker = "通知栏提示文字",
title = title,
text = "通知文字描述",
after_open = "go_app"
//url = "",
//activity = model.activity == null ? "" : model.activity
},
},
policy = new
{
start_time = ""
}
};
}
else if (member_platform == "ios")
{
appkey = "appkey";
app_master_secret = "app_master_secret";
param = new
{
appkey = appkey,
timestamp = timestamp,
type = "customizedcast",
alias_type = "UserGroup",
alias = member_id,
payload = new
{
aps = new // 必填 严格按照APNs定义来填写
{
alert = title // 必填
}
},
policy = new
{
start_time =""
},
production_mode = "false"
};
}
string url = "http://msg.umeng.com/api/send";
var requestJson = JsonConvert.SerializeObject(param);
string mysign = getMD5Hash("POST" + url + requestJson + app_master_secret);
url = url + "?sign=" + mysign;
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
byte[] bs = Encoding.UTF8.GetBytes(requestJson);
request.ContentLength = bs.Length;
using (Stream reqStream = request.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
reqStream.Close();
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
HttpStatusCode statusCode = response.StatusCode;
if (statusCode == System.Net.HttpStatusCode.OK)
{
o = new { code = 1000, data = new { isExist = isExist }, msg = "发送成功" };
}
else
{
o = new { code = 1000, data = new { isExist = isExist }, msg = "发送失败" };
}
}
catch (WebException e)
{
o = new { code = 3000, data = new { }, msg = e.Message };
}
string json = JsonConvert.SerializeObject(o);
result = new HttpResponseMessage { Content = new StringContent(json, Encoding.GetEncoding("UTF-8"), "application/json") };
return result;
}
public static String getMD5Hash(String str)
{
System.Security.Cryptography.MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(str));
StringBuilder strReturn = new StringBuilder();
for (int i = 0; i < result.Length; i++)
{
strReturn.Append(Convert.ToString(result[i], 16).PadLeft(2, '0'));
}
return strReturn.ToString().PadLeft(32, '0');
}