public class LYMMsgPush
{
private static string AppKey = "";
private static string Secret = "";
private static ILog logServices = LogManager.GetLogger(typeof(LYMMsgPush));
/// <summary>
/// 根据用户推送消息
/// </summary>
/// <param name="msg">消息内容</param>
/// <param name="userids">推送用户登录名集合 不能超过1000个</param>
/// <returns></returns>
private static PushPayload PushMsgInfoByUsers(string msgtitle, string msgcontent, Dictionary<string, string> lst, params string[] ids)
{
PushPayload pushPayload = new PushPayload();
pushPayload.platform = Platform.android();
pushPayload.audience = Audience.s_alias(ids);
pushPayload.message = Message.content(msgcontent).setTitle(msgtitle);
foreach (var item in lst)
{
pushPayload.message.AddExtras(item.Key, item.Value);
}
return pushPayload;
}
/// <summary>
/// 按组别推送消息
/// </summary>
/// <param name="msg">推送内容</param>
/// <param name="areaCodes">区域编码 不能超过1000个</param>
/// <returns></returns>
private static PushPayload PushMsgInfoByAreas(string msgtitle, string msgcontent, Dictionary<string, string> lst, params string[] ids)
{
PushPayload pushPayload = new PushPayload();
pushPayload.platform = Platform.android();
pushPayload.audience = Audience.s_segment(ids);
pushPayload.message = Message.content(msgcontent).setTitle(msgtitle);
foreach (var item in lst)
{
pushPayload.message.AddExtras(item.Key, item.Value);
}
return pushPayload;
}
private static PushPayload PushMsgInfoByAll(string msgtitle, string msgcontent, Dictionary<string, string> lst)
{
PushPayload pushPayload = new PushPayload();
pushPayload.platform = Platform.android();
pushPayload.audience = Audience.all();
pushPayload.message = Message.content(msgcontent).setTitle(msgtitle);
foreach (var item in lst)
{
pushPayload.message.AddExtras(item.Key, item.Value);
}
return pushPayload;
}
/// <summary>
/// 消息推送服务(自定义消息) 组别和别名推送
/// </summary>
/// <param name="type">类型: 1:推送到人,2:推送到组别 3:所有设备提示</param>
/// <param name="msgtitle">消息标题</param>
/// <param name="msgcontent">消息内容</param>
/// <param name="lst">自定义消息内容 键值对</param>
/// <param name="ids">推送标识(可以是推送用户登录名集合,也可以是用户区域集合)</param>
public static void SendMsg(int type, string msgtitle, string msgcontent, Dictionary<string, string> lst, params string[] ids)
{
PushPayload payload = null;
if (string.IsNullOrEmpty(AppSetting.AppSettingsHelper.GetString("publish")))
{
lst = lst != null ? lst : new Dictionary<string, string>();
lst.Add("test", "true");
}
if (type == 1)
{
payload = PushMsgInfoByUsers(msgtitle, msgcontent, lst, ids);
}
if (type == 2)
{
payload = PushMsgInfoByAreas(msgtitle, msgcontent, lst, ids);
}
if (type == 3)
{
payload = PushMsgInfoByAll(msgtitle, msgcontent, lst);
}
try
{
JPushClient client = new JPushClient(AppKey, Secret);
var result = client.SendPush(payload);
}
catch (APIRequestException e)
{
logServices.Error("HTTP Status: " + e.Status + "
Error Code: " + e.ErrorCode + "
Error Message: " + e.ErrorMessage);
throw new Exception(e.Message);
}
catch (APIConnectionException e)
{
logServices.Error(e.Message);
throw new Exception(e.Message);
}
}
}