Class JPushClient
This class is the core for calling JPush API via official RESTful interfaces. There are two major features:
- Send Push message for iOS and/or Android
- Track delivery status via multiple message ID, which returned by JPush Service.
Sample:
Here is a sample based on .NET console application.
namespace ifunction.JPush.Test { staticclass Program { staticvoid Main(string[] args) { var appKey = "1234567890abcdef"; // Your App Key from JPushvar masterSecret = "1234567890abcdef"; // Your Master Secret from JPush Dictionary<string, string> customizedValues = new Dictionary<string, string>(); customizedValues.Add("CK1", "CV1"); customizedValues.Add("CK2", "CV2"); JPushHandler handler = new JPushHandler( appKey, // Your app key masterSecret, // Your master secret CreatePushMessageLogDelegate, // Delegate for creating push message log. Generally, it is to save log in database or any other media for later tracking in UI, but to make it simple here, just console write lines of information in JSON format. UpdatePushMessageStatus, // Delegate for updating push message status. Generally, it is to update status in database, but to make it simple here, just console write lines of information.null, // Delegate for initializing message tracking information. Generally, it is to read message id collection which need to be tracked from database, especially it occurs when machine is rebooted. To make it simple here, leave null for doing nothing. null, // Delegate for reporting exception. Generally, it is to write log in file, event log or database. To make it simple here, leave null for doing nothing.2// Interval value for monitoring thread for get update for message status. By default, it is 60 in seconds. It is set 2 seconds here for sample. ); handler.SendPushMessage(new PushMessageRequest { MessageType = MessageType.Notification, Platform = PushPlatform.Android, Description = "DotNET", PushType = PushType.Broadcast, IsTestEnvironment = true, Message = new PushMessage { Content = "Hello, this is a test push from .NET. Have a nice day!", PushTitle = "A title.", Sound = "YourSound", CustomizedValue = customizedValues } }); Console.WriteLine("Tracking IDs in list:"); foreach (var one in handler.MessageTrackingId) { Console.WriteLine(one); } Console.WriteLine("---------- END ----------"); Thread.Sleep(10 * 1000); // To sleep 10 seconds here to make sure the monitoring thread in hander has been invoked. Console.WriteLine("Press any key to exit."); Console.Read(); } publicstaticvoid CreatePushMessageLogDelegate(PushMessageRequest messageRequest, PushResponse response) { Console.WriteLine(string.Format(@"Push Log {0} Request: {1} Response: {2}", JsonConvert.SerializeObject(messageRequest), JsonConvert.SerializeObject(response))); } publicstaticvoid CreatePushMessageLog(PushMessageRequest messageRequest, PushResponse response) { Console.WriteLine(string.Format(@"Push Log {0} Request: {1} Response: {2}", JsonConvert.SerializeObject(messageRequest), JsonConvert.SerializeObject(response))); } publicstaticvoid UpdatePushMessageStatus(List<PushMessageStatus> pushMessageStatus) { if (pushMessageStatus != null) { foreach (var one in pushMessageStatus) { Console.WriteLine(string.Format(@"Push Message Status Update: {0} Android: {1} iOS: {2}", one.MessageId, one.AndroidDeliveredCount, one.ApplePushNotificationDeliveredCount)); } } } } } |
RESTful API reference: http://docs.jpush.cn/display/dev/Index
NOTE:
- As official document defined, the message ID collection is limited by 1000 in one track request. So if more than 1000 is set in collection for tracking, only the first 1000 can get status information in result.
- Regarding to the possible change of official RESTful API, current client is cliamed to base on v2 version of JPush.