Fork me on GitHub

极光推送

 public class SMSData
    {

        public string msg_id { get; set; }
        public string is_valid { get; set; }
        public ErrorMsg error { get; set; }
    }
    public class ErrorMsg
    {
        public string code { get; set; }
        public string message { get; set; }
    }
    public class LYMJSMSPush
    {

        private static string AppKey = "";
        private static string Secret = "";


        public static SMSData SendSMSVoice(string phone, int code)
        {
            JSMSClient client = new JSMSClient(AppKey, Secret);
            HttpResponse response = client.SendVoiceCodeAsync(new VoiceCode { Code = code, Languarge = 0, Life = 120, Mobile = phone }).Result;
            SMSData data = Newtonsoft.Json.JsonConvert.DeserializeObject<SMSData>(response.Content);
            return data;
        }


     

        public static SMSData SendSMS(string phone)
        {
            JSMSClient client = new JSMSClient(AppKey, Secret);
            HttpResponse response = client.SendCodeAsync(phone, 1).Result;
            SMSData data = Newtonsoft.Json.JsonConvert.DeserializeObject<SMSData>(response.Content);
            return data;

        }

        public static SMSData ValidatorCode(string msg_id, string code)
        {
            JSMSClient client = new JSMSClient(AppKey, Secret);
            HttpResponse response = client.IsCodeValidAsync(msg_id, code).Result;
            SMSData data = Newtonsoft.Json.JsonConvert.DeserializeObject<SMSData>(response.Content);
            if (data.is_valid == "false")
            {
                if (data.error.code == "50010")
                {
                    data.error.message = "验证码无效";
                }
                if (data.error.code == "50011")
                {
                    data.error.message = "验证码过期";
                }
                if (data.error.code == "50012")
                {
                    data.error.message = "验证码已验证通过";
                }
                if (data.error.code == "50014")
                {
                    data.error.message = "可发短信余量不足";
                }
                if (data.error.code == "50015")
                {
                    data.error.message = "验证码为空";
                }
                if (data.error.code == "50034")
                {
                    data.error.message = "重复发送";
                }
            }





            return data;

        }



    }
短信篇
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 + "\r\nError Code: " + e.ErrorCode + "\r\nError Message: " + e.ErrorMessage);
                throw new Exception(e.Message);

            }
            catch (APIConnectionException e)
            {
                logServices.Error(e.Message);
                throw new Exception(e.Message);
            }

        }



    }
App端提示

 

posted @ 2017-10-11 13:16  龙码精神  阅读(403)  评论(0编辑  收藏  举报