Class JPushHandler
This class is based on class JPushClient. The features and the constructor are similar but it would create a singleton background thread to call JPush automatically for updating status of messages. Generally, the delegates in constructor are all based on database action, but you can customize them all as you can or even leave them null.

 

If you want to get an entire solution for handling JPush sending and monitoring, please refer JPush Service class library and demo for more information.

 

Sample:

Here is a sample based on .NET console application.

 namespace ifunction.JPush.Test
{
    static class Program
    {
        static void Main(string[] args)
        {
            var appKey = "1234567890abcdef"; // Your App Key from JPush
            var 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();
        }

        public static void CreatePushMessageLogDelegate(PushMessageRequest messageRequest, PushResponse response)
        {
            Console.WriteLine(string.Format(@"Push Log {0}
Request:
{1}

Response:
{2}", JsonConvert.SerializeObject(messageRequest), JsonConvert.SerializeObject(response)));
        }

        public static void CreatePushMessageLog(PushMessageRequest messageRequest, PushResponse response)
        {
            Console.WriteLine(string.Format(@"Push Log {0}
Request:
{1}
Response:
{2}", JsonConvert.SerializeObject(messageRequest), JsonConvert.SerializeObject(response)));
        }

        public static void 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.

Last edited May 20, 2014 at 10:47 AM by rynnwang, version 3