private void OrderTimeoutdalRun(object o) { if (_glb == null) { _glb = new JushGoldBookRemind_DAL(); } if (payload == null) { payload = new PushPayload(); } list = _glb.GoldOutList(); if (list.Count > 0) { List <string> list_users = new List <string>(); list.ForEach(z => list_users.Add(z.UsedId.ToString().DESEncrypt().StringToMd5().Md532SubString())); //PushPayload payload = new PushPayload(); payload.platform = Platform.all(); payload.audience = Audience.s_alias(list_users.ToArray()); payload.notification = new Notification().setAlert(Config.JushGoldBookRemindRemingMsg).setAndroid(new AndroidNotification().AddExtra("key", Config.JushGoPageGoldBook)).setIos(new IosNotification().AddExtra("key", Config.JushGoPageGoldBook)); try { client.SendPush(payload); //记录日志 Config.PushLogAPP("APP优惠卷超时提醒", list_users.ListToStrings(), Config.JushGoldBookRemindRemingMsg); } catch (Jpush.api.common.APIRequestException ee) { string.Format("\r\n>>>APP优惠卷超时提醒 推送异常:{0},time:{1}", ee.Message, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") ).WriteLog("ERROR"); } } }
private bool SendPush(PushPayload pushOption) { try { JPushClient client = new JPushClient(appKey, masterSecret); var result = client.SendPush(pushOption); if (result.ResponseResult.responseCode != System.Net.HttpStatusCode.OK) { throw new ArgumentException(result.ResponseResult.exceptionString); } return(result.ResponseResult.responseCode == System.Net.HttpStatusCode.OK); } catch (APIRequestException ex) { new CustomException(string.Format("JPushHelper--SendPush--APIRequestException--Message:{0},ErrorMessage:{1},ErrorCode:{2},", ex.Message, ex.ErrorMessage, ex.ErrorCode)); } catch (APIConnectionException ex) { new CustomException(string.Format("JPushHelper--SendPush--APIConnectionException--Message:{0}", ex.Message)); } catch (Exception ex) { new CustomException("JPushHelper--SendPush--Exception--", ex); } return(false); }
/// <summary> /// 设置短信推送信息 /// </summary> /// <param name="message">推送内容</param> /// <param name="registrationIds">管理员的手机注册码</param> public static void PushMessage(string message, List <string> registrationIds) { JPushClient client = new JPushClient(AppKey, MasterSecret); PushPayload pushPayload = new PushPayload() { Platform = new List <string> { "android", "ios" }, Audience = new Audience() { RegistrationId = registrationIds }, Notification = new Notification { Alert = message, }, Options = new Options { //IsApnsProduction = true // 设置 iOS 推送生产环境。不设置默认为开发环境。 } }; var response = client.SendPush(pushPayload); NLog.LogManager.GetCurrentClassLogger().Info(response.Content); }
private static void ExecutePushExample() { PushPayload pushPayload = new PushPayload() { Platform = "android", Audience = "all", Notification = new Notification() { Alert = "hello jpush", Android = new Android() { Alert = "android alert", Title = "title" }, IOS = new IOS() { Alert = "ios alert", Badge = "+1" } }, Message = new Message() { Title = "message title", Content = "message content", Extras = new Dictionary <string, string>() { ["key1"] = "value1" } } }; var response = client.SendPush(pushPayload); Console.WriteLine(response.Content); }
//private RequestParamModel<MessageModel> GetRpm(FormCollection context) //{ // var json = context["json"]; // var rpm = new RequestParamModel<MessageModel>(context) { CurrentContext = context, Action = Request["action"] }; // if (!string.IsNullOrEmpty(json)) // { // rpm = JSONhelper.ConvertToObject<RequestParamModel<MessageModel>>(json); // rpm.CurrentContext = context; // } // return rpm; //} //public ActionResult Send() //{ // ViewBag.ToolBar = BuildToolbar(); // return View(); //} //[HttpPost] //[ValidateInput(false)] //public string Send(FormCollection context) //{ // try // { // var ids = Request["ids"]; // var title = Request["title"]; // var content = Request["content"]; // if (string.IsNullOrEmpty(ids)) // { // try // { // List<AppUserModel> list = AppUserDal.Instance.GetWhereStr(" and MASTER_ID IS NOT NULL").ToList(); // foreach (AppUserModel userModel in list) // { // MessageModel messageModel = new MessageModel(); // messageModel.RECEIVE_ID = 0;// PublicMethod.GetDecimal(id); // // model.CONTENT = content; // messageModel.HTML_CONTENT = content; // messageModel.IS_RREAD = 0; // messageModel.SEND_TIME = DateTime.Now; // messageModel.TITLE = title; // messageModel.USER_ID = userModel.FID; // MessageDal.Instance.Insert(messageModel); // } // JPushClient client = new JPushClient(SysVisitor.Instance.PushAppKey, SysVisitor.Instance.PushMasterSecret); // PushPayload pushPayload = new PushPayload(); // pushPayload.platform = Platform.all(); // pushPayload.audience = Audience.all(); // pushPayload.notification = new Notification().setAlert(title); // client.SendPush(pushPayload); // } // catch (Exception ex) // { // LogHelper.WriteLog(string.Format("Error: appKey={0},masterSecret={1},phone={2},title={3},msg={4}", SysVisitor.Instance.PushAppKey, SysVisitor.Instance.PushMasterSecret, "全体", title, ex.Message)); // } // } // else // { // string[] array = ids.Split(','); // foreach (string id in array) // { // MessageModel model = new MessageModel(); // model.RECEIVE_ID = 0;// PublicMethod.GetDecimal(id); // // model.CONTENT = content; // model.HTML_CONTENT = content; // model.IS_RREAD = 0; // model.SEND_TIME = DateTime.Now; // model.TITLE = title; // model.USER_ID = id; // if (MessageDal.Instance.Insert(model) != "") // { // OnPushMessage(id, title); // } // } // } // return JSONhelper.ToJson("发送成功!"); // } // catch (Exception ex) // { // return JSONhelper.ToJson(ex.Message); // } //} private void OnPushMessage(string id, string title) { //DriverModel model = DriverDal.Instance.GetByID(id); AppUserModel model = AppUserDal.Instance.Get(id); if (model != null) { try { JPushClient client = new JPushClient(SysVisitor.Instance.PushAppKey, SysVisitor.Instance.PushMasterSecret); PushPayload pushPayload = new PushPayload(); pushPayload.platform = Platform.all(); pushPayload.audience = Audience.s_alias(model.PHONE); pushPayload.notification = new Notification().setAlert(title); client.SendPush(pushPayload); } catch (Exception ex) { LogHelper.WriteLog(string.Format("Error: appKey={0},masterSecret={1},phone={2},title={3},msg={4}", SysVisitor.Instance.PushAppKey, SysVisitor.Instance.PushMasterSecret, model.PHONE, title, ex.Message)); } } }
/// <summary> /// 推送消息 /// </summary> /// <param name="notificationTitle">通知title</param> /// <param name="content">通知内容</param> /// <param name="message">消息内容</param> /// <param name="deviceTypes">推送平台</param> /// <param name="pushId">用户标识</param> /// <param name="keyValueParams">参数</param> /// <returns>推送消息ID</returns> public static long SendPush( string notificationTitle, string content, string message, DeviceType deviceTypes, string pushId, Dictionary <string, string> keyValueParams) { PushPayload pushPayload = new PushPayload(); var platformNotification = GetPlatformNotification(deviceTypes, notificationTitle, content, keyValueParams); pushPayload.platform = platformNotification.Item1; pushPayload.notification = platformNotification.Item2; Audience audience = string.IsNullOrWhiteSpace(pushId) ? Audience.all() : Audience.s_registrationId(pushId); pushPayload.audience = audience; if (!string.IsNullOrWhiteSpace(message)) { pushPayload.message = Message.content(message); } //true 表示推送生产环境, false表示要推送开发环境; pushPayload.ResetOptionsApnsProduction(isRelease); var result = _client.SendPush(pushPayload); return(result.msg_id); }
/// <summary> /// 发送消息 /// </summary> /// <param name="msg">消息</param> /// <param name="appKey">appkey</param> /// <param name="masterSecret">主密钥</param> private static void sendAndroidMsgToUsers(PushMsgModel msg, List <PushStaff> userDics, string appKey = "9b725db3484eceb1abab684a", string masterSecret = "7a6081367b0d0dda83a8b969") { JPushClient client = new JPushClient(appKey, masterSecret); PushPayload pushPayload = new PushPayload(); pushPayload.platform = Platform.android(); foreach (PushStaff item in userDics) { pushPayload.audience = Audience.s_alias(item.UserName); pushPayload.options.time_to_live = 30; if (item.IsPushVoice) { msg.IsPushVoice = true; pushPayload.message = Message.content(JsonConvert.SerializeObject(msg)); } else { msg.IsPushVoice = false; pushPayload.message = Message.content(JsonConvert.SerializeObject(msg)); } try { var result = client.SendPush(pushPayload); } catch { } } }
public static void PushMessage(string telephone, string message) { PushPayload payload = new PushPayload(); payload.platform = Platform.all(); payload.audience = Audience.s_tag(telephone); payload.notification = new Notification().setAlert(message); ThreadPool.QueueUserWorkItem(new WaitCallback((obj) => { try { client.SendPush((PushPayload)obj); var db = Data.Entities.NewInstance; var notice = new Data.notice() { content = message, isread = false, noticetime = DateTime.Now, noticetype = 1, telephone = telephone }; db.notices.Add(notice); db.SaveChanges(); LogHelper.LogInfo(string.Format("消息推送成功(收件人:{0} 消息:{1})", telephone, message)); } catch (Exception e) { LogHelper.LogError(e); } }), payload); }
//public static String TITLE = "Test from C# v3 sdk"; //public static String ALERT = "Test from C# v3 sdk - alert"; //public static String MSG_CONTENT = "Test from C# v3 sdk - msgContent"; //public static String REGISTRATION_ID = "0900e8d85ef"; //public static String TAG = "tag_api"; //public static String app_key = "17a5eb963edcbd8ef0d954e4"; //public static String master_secret = "4b6d3a888eba4ab44c12af36"; /// <summary> /// /// </summary> /// <param name="title">标题</param> /// <param name="alert">标题</param> /// <param name="msgcontent">内容</param> /// <param name="tag">tag 多条“,”隔开</param> /// <param name="alias">alias 多条“,”隔开</param> /// <param name="type">推送类型</param> public static int Main(string title, string alert, string alias, string activity = "", string app_key = "", string master_secret = "") { string REGISTRATION_ID = "0900e8d85ef"; //string app_key = "17a5eb963edcbd8ef0d954e4"; //string master_secret = "4b6d3a888eba4ab44c12af36"; //if(activity == MTConfig.ActivityName.教师拼单未开始) //{ // List<XUserModel> list = UserModel.Fetch("where User_Type = @0", MTConfig.UserType.Teacher); // alias = string.Join(",", list.Select(s => s.Id)); // if(string.IsNullOrEmpty(alias)) // { // alias = "0"; // } //} //if (activity == MTConfig.ActivityName.教师需求单未开始) //{ // List<XUserModel> list = UserModel.Fetch("where User_Type = @0", MTConfig.UserType.Teacher); // alias = string.Join(",", list.Select(s => s.Id)); // if (string.IsNullOrEmpty(alias)) // { // alias = "0"; // } //} JPushClient client = new JPushClient(app_key, master_secret); PushPayload payload = new PushPayload(); if (string.IsNullOrEmpty(alias)) { payload = PushObject_All_All_Alert(alert, title, "", activity); } else { payload = PushObject_All_All_Alert(alert, title, alias, activity); } try { var result = client.SendPush(payload); //由于统计数据并非非是即时的,所以等待一小段时间再执行下面的获取结果方法 System.Threading.Thread.Sleep(10000); /*如需查询上次推送结果执行下面的代码*/ var apiResult = client.getReceivedApi(result.msg_id.ToString()); var apiResultv3 = client.getReceivedApi_v3(result.msg_id.ToString()); /*如需查询某个messageid的推送结果执行下面的代码*/ var queryResultWithV2 = client.getReceivedApi("1739302794"); var querResultWithV3 = client.getReceivedApi_v3("1739302794"); return(1); } catch (APIRequestException e) { Console.WriteLine("Error response from JPush server. Should review and fix it. "); Console.WriteLine("HTTP Status: " + e.Status); Console.WriteLine("Error Code: " + e.ErrorCode); Console.WriteLine("Error Message: " + e.ErrorCode); return(e.ErrorCode); } }
/// <summary> /// 极光推送方法 /// </summary> /// <param name="model"></param> public static void PushMessage(JPushModel model) { try { string appKey = ""; string masterSecret = ""; if (model.TagId == 0) //C端 { appKey = "dce902893245e99461b9a5c8"; // Your App Key from JPush masterSecret = "fdc95d37d67c9472ad4e0e96"; // Your Master Secret from JPush } else if (model.TagId == 1) //B端 { appKey = "d794d51f2ffaf5de42001c4b"; // Your App Key from JPush masterSecret = "03f956afaaeb086481aa3b7c"; // Your Master Secret from JPush } JPushClient client = new JPushClient(appKey, masterSecret); Audience audience = null; if (model.PushType == 0) { //0:标签,因为一个应用只能有一个标签,现有支付已经使用,其它应用请使用别名 audience = Audience.s_alias(model.RegistrationId); model.ContentKey = "Content"; } if (model.PushType == 1) { //1:别名 audience = Audience.s_tag(model.RegistrationId); } PushPayload pushPayload = new PushPayload(); pushPayload.platform = Platform.android_ios(); pushPayload.audience = audience; Notification notification = new Notification().setAlert(model.Alert);//不需要写弹出内容 notification.AndroidNotification = new AndroidNotification().setTitle(model.Title); notification.IosNotification = new IosNotification().setAlert(model.Alert).setBadge(1).setSound(string.Concat(model.ContentKey, ":", model.Content)); if (!string.IsNullOrEmpty(model.Content)) { //notification.IosNotification = new IosNotification().setAlert(model.Alert).setBadge(1).setSound("YourSound").AddExtra(model.ContentKey, model.Content); notification.AndroidNotification = new AndroidNotification().AddExtra(model.ContentKey, model.Content); } pushPayload.notification = notification.Check(); var response = client.SendPush(pushPayload); if (!response.isResultOK()) { LogHelper.LogWriter("推送失败", response.msg_id); } else { LogHelper.LogWriter("推送成功", response.msg_id); } } catch (Exception ex) { string parm = string.Concat("推送异常,参数:tagId", model.TagId, ",RegistrationId:", model.RegistrationId); LogHelper.LogWriter(ex, parm); } }
/// <summary> /// 极光推送主程序 /// </summary> /// <param name="_app_key">极光app_key</param> /// <param name="_master_secret">极光master_secret</param> /// <param name="title">通知标题(只支持Android)</param> /// <param name="alert">通知内容</param> /// <param name="shebeitype">设备类型(0-Android,1-IOS)</param> /// <param name="extra">参数字典</param> /// <param name="registrationid">设备标识(为空则推送全部用户)</param> /// <returns></returns> public static MessageResult SendPushJiGuang(string title, string alert, int?shebeitype, Dictionary <string, object> extra, HashSet <string> alias) { //logger.Info("*****开始发送******"); try { JPushClient client = new JPushClient(app_key, master_secret); PushPayload pushPayload = new PushPayload(); pushPayload.platform = Platform.android_ios(); if (alias != null && alias.Count() > 0) { pushPayload.audience = Audience.s_alias(alias); } else { pushPayload.audience = Audience.all(); } var notification = new Notification().setAlert(alert); //if (shebeitype == 0) //{ notification.AndroidNotification = new AndroidNotification(); notification.AndroidNotification.setTitle(title); if (extra != null && extra.Count > 0) { foreach (var item in extra) { notification.AndroidNotification.AddExtra(item.Key, item.Value.ToString()); } } //} //else //{ notification.IosNotification = new IosNotification(); notification.IosNotification.disableBadge(); notification.IosNotification.setBadge(0); notification.IosNotification.setSound("default"); if (extra != null && extra.Count > 0) { foreach (var item in extra) { notification.IosNotification.AddExtra(item.Key, item.Value.ToString()); } } //} pushPayload.notification = notification.Check(); var result = client.SendPush(pushPayload); return(result); } catch (APIRequestException e) { logger.ErrorFormat("推送失败:从jpush服务器错误响应。检查并修复它. <br>HTTP状态:{0}<br>错误代码:{1}<br>错误消息:{2}", e.Status, e.ErrorCode, e.ErrorMessage); } catch (APIConnectionException e) { logger.Error("推送失败" + e.Message); } return(null); }
/// <summary> /// 发送推送 /// </summary> /// <param name="alert">推送内容</param> /// <param name="MemberIds">推送到的用户用逗号分隔, 如果留空则为全部用户</param> /// <param name="MerId">商家ID</param> public void SendPush(string alert, string MemberIds, string eventStr, decimal MerId, string[] tag) { string[] sa = { "JPushAppKey", "JPushMasterSecret" }; Dictionary <string, string> MerConfig = BLL.StaticBLL.MerConfig(MerId, sa); JPushClient client = new JPushClient(MerConfig["JPushAppKey"], MerConfig["JPushMasterSecret"]); PushPayload pushPayload = new PushPayload(); pushPayload.platform = Platform.all(); var notification = new Notification().setAlert(alert); AndroidNotification androidNotification = new AndroidNotification(); IosNotification iosNotification = new IosNotification(); Options options = new Options(); options.apns_production = true; //生产环境的 pushPayload.options = options; androidNotification.AddExtra("eventStr", eventStr); iosNotification.AddExtra("eventStr", eventStr); notification.setAndroid(androidNotification); notification.setIos(iosNotification); if (MemberIds.Trim() != "") { //如果不为空,说明指定了MemberId string[] MemberArray = MemberIds.Split(','); pushPayload.audience = Audience.s_alias(MemberArray); //推送设备对象,表示一条推送可以被推送到那些设备,确认推送设备的对象,JPush提供了多种方式,比如:别名,标签,注册id,分群,广播等。 } else if (tag != null) { if (tag.Length > 0) { pushPayload.audience = Audience.s_tag(tag); //按照标签推送 } } else { pushPayload.audience = Audience.all();//推送设备对象,表示一条推送可以被推送到那些设备,确认推送设备的对象,JPush提供了多种方式,比如:别名,标签,注册id,分群,广播等。 } pushPayload.notification = notification; pushPayload.message = Message.content("msg") .AddExtras("DoEvent", "GetNewMsgNum()"); //如果不加一条自定义消息的话, android是不会触发监听事件的.但是IOS可以 var result = client.SendPush(pushPayload); }
public void test_empty_string() { JPushClient pushClient = new JPushClient(APP_KEY, MASTER_SECRET); try { pushClient.SendPush(""); } catch (ArgumentException e) { Debug.WriteLine(e.Message); } }
public void test_validate() { JPushClient pushClient = new JPushClient(APP_KEY, MASTER_SECRET); try { var result = pushClient.SendPush(PushPayload.AlertAll("alert")); Assert.IsTrue(result.isResultOK()); } catch (Exception e) { Debug.WriteLine(e.Message); } }
public static void Main(string[] args) { Console.WriteLine("*****开始发送******"); JPushClient client = new JPushClient(app_key, master_secret); PushPayload payload = PushObject_All_All_Alert(); try { var result = client.SendPush(payload); //由于统计数据并非非是即时的,所以等待一小段时间再执行下面的获取结果方法 System.Threading.Thread.Sleep(10000); //如需查询上次推送结果执行下面的代码 var apiResult = client.getReceivedApi(result.msg_id.ToString()); var apiResultv3 = client.getReceivedApi_v3(result.msg_id.ToString()); //如需查询某个messageid的推送结果执行下面的代码 var queryResultWithV2 = client.getReceivedApi("1739302794"); var querResultWithV3 = client.getReceivedApi_v3("1739302794"); } catch (APIRequestException e) { Console.WriteLine("Error response from JPush server. Should review and fix it. "); Console.WriteLine("HTTP Status: " + e.Status); Console.WriteLine("Error Code: " + e.ErrorCode); Console.WriteLine("Error Message: " + e.ErrorMessage); } catch (APIConnectionException e) { Console.WriteLine(e.Message); } //send smsmessage PushPayload pushsms = PushSendSmsMessage(); try { var result = client.SendPush(pushsms); //由于统计数据并非非是即时的,所以等待一小段时间再执行下面的获取结果方法 System.Threading.Thread.Sleep(10000); //如需查询上次推送结果执行下面的代码 var apiResult = client.getReceivedApi(result.msg_id.ToString()); var apiResultv3 = client.getReceivedApi_v3(result.msg_id.ToString()); //如需查询某个messageid的推送结果执行下面的代码 var queryResultWithV2 = client.getReceivedApi("1739302794"); var querResultWithV3 = client.getReceivedApi_v3("1739302794"); } catch (APIRequestException e) { Console.WriteLine("Error response from JPush server. Should review and fix it. "); Console.WriteLine("HTTP Status: " + e.Status); Console.WriteLine("Error Code: " + e.ErrorCode); Console.WriteLine("Error Message: " + e.ErrorMessage); } catch (APIConnectionException e) { Console.WriteLine(e.Message); } PushPayload payload_alias = PushObject_all_alias_alert(); try { var result = client.SendPush(payload_alias); //由于统计数据并非非是即时的,所以等待一小段时间再执行下面的获取结果方法 System.Threading.Thread.Sleep(10000); //如需查询上次推送结果执行下面的代码 var apiResult = client.getReceivedApi(result.msg_id.ToString()); var apiResultv3 = client.getReceivedApi_v3(result.msg_id.ToString()); //如需查询某个messageid的推送结果执行下面的代码 var queryResultWithV2 = client.getReceivedApi("1739302794"); var querResultWithV3 = client.getReceivedApi_v3("1739302794"); } catch (APIRequestException e) { Console.WriteLine("Error response from JPush server. Should review and fix it. "); Console.WriteLine("HTTP Status: " + e.Status); Console.WriteLine("Error Code: " + e.ErrorCode); Console.WriteLine("Error Message: " + e.ErrorMessage); } catch (APIConnectionException e) { Console.WriteLine(e.Message); } Console.WriteLine("*****结束发送******"); }
public void test_invalid_json() { JPushClient pushClient = new JPushClient(APP_KEY, MASTER_SECRET); pushClient.SendPush("{aaa:'a}"); }