/// <summary> /// Initializes a new instance of the <see cref="JPushClient" /> class. /// </summary> /// <param name="appKey">The application key.</param> /// <param name="masterSecret">The master secret.</param> /// <param name="pushResultUpdate">The update push message status delegate.</param> /// <param name="pushTrackingInitialize">The initialize delegate.</param> /// <param name="exceptionReport">The exception delegate.</param> /// <param name="threadInterval">The interval in second.</param> public JPushHandler(string appKey, string masterSecret, int threadInterval = 60, UpdatePushResult pushResultUpdate = null, InitializePushTracking pushTrackingInitialize = null, ReportException exceptionReport = null) { JPushClient = new JPushClient(appKey, masterSecret, true); if (threadInterval < 1) { threadInterval = 60; } this.ThreadInterval = threadInterval; this.PushTrackings = new List <Tracking>(); this.DataLocker = new object(); this.PushResultUpdate = pushResultUpdate; this.PushTrackingInitialize = pushTrackingInitialize; this.ExceptionReport = exceptionReport; this.InitializeMonitorThreadInBackground(); }
/// <summary> /// 监控功能 /// </summary> protected void MonitorFunction() { while (true) { try { List <string> ids = new List <string>(); lock (DataLocker) { DateTime nowUtcTime = DateTime.UtcNow; for (int i = 0; i < this.PushTrackings.Count;) { Tracking pushTracking = this.PushTrackings[i]; if ((nowUtcTime - pushTracking.UtcTimestamp).TotalMinutes > PUSHTRACKINGLIFETIME) { this.PushTrackings.RemoveAt(i); } else { ids.Add(pushTracking.MessageID); i++; } } } List <Result> results = new List <Result>(); while (ids.Count > 0) { List <string> listToOperate = new List <string>(); listToOperate.AddRange(ids.Take(ids.Count > 100 ? 100 : ids.Count)); IList <Result> queryResult = JPushClient.QueryResult(listToOperate); if (queryResult != null) { results.AddRange(queryResult); } ids.RemoveRange(0, listToOperate.Count); } if (this.PushResultUpdate != null) { this.PushResultUpdate.Invoke(results); } } catch (Exception e) { this.HandleException(new Exception("Failed to run monitor work.", e)); } Thread.Sleep(this.ThreadInterval * 1000); } }
/// <summary> /// Send the message push. /// </summary> /// <param name="request"></param> /// <returns></returns> public Response Push(Request request) { try { Response response = JPushClient.Push(request); if (response != null && !string.IsNullOrWhiteSpace(response.MessageID)) { this.AddMessageTrackingID(new Tracking { MessageID = response.MessageID, UtcTimestamp = DateTime.UtcNow }); } return(response); } catch (Exception e) { this.HandleException(new Exception("Failed to send push message", e)); } return(null); }
/// <summary> /// Initializes a new instance of the <see cref="JPushClient" /> class. /// </summary> /// <param name="appKey">The application key.</param> /// <param name="masterSecret">The master secret.</param> /// <param name="pushResultUpdate">The update push message status delegate.</param> /// <param name="pushTrackingInitialize">The initialize delegate.</param> /// <param name="exceptionReport">The exception delegate.</param> /// <param name="threadInterval">The interval in second.</param> public JPushHandler(string appKey, string masterSecret, int threadInterval = 60, UpdatePushResult pushResultUpdate = null, InitializePushTracking pushTrackingInitialize = null, ReportException exceptionReport = null) { JPushClient = new JPushClient(appKey, masterSecret, true); if (threadInterval < 1) threadInterval = 60; this.ThreadInterval = threadInterval; this.PushTrackings = new List<Tracking>(); this.DataLocker = new object(); this.PushResultUpdate = pushResultUpdate; this.PushTrackingInitialize = pushTrackingInitialize; this.ExceptionReport = exceptionReport; this.InitializeMonitorThreadInBackground(); }