Quantcast
Channel: jpush Wiki Rss Feed
Viewing all articles
Browse latest Browse all 21

Updated Wiki: class: JPushClientV3

$
0
0

Class JPushClientV3 
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 and/or Windows Phone
  • Track delivery status via multiple message ID, which returned by JPush Service.

 

Sample:

Here is a sample based on .NET console application.

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using ifunction.JPush.V3;
using Newtonsoft.Json;
namespace ifunction.JPush.Test
{
    class 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");

            JPushClientV3 client = new JPushClientV3(appKey, masterSecret);

            Audience audience = new Audience();
            // In JPush V3, tag can be multiple added with different values.// In following code, it is to send push to those who are in ((Tag1 AND Tag2) AND (Tag3 OR Tag4))// If you want to send to all, please use: audience.Add(PushTypeV3.Broadcast, null);
            audience.Add(PushTypeV3.ByTagWithinAnd, new List<string>(newstring[] { "Tag1", "Tag2" }));
            audience.Add(PushTypeV3.ByTagWithinOr, new List<string>(newstring[] { "Tag3", "Tag4" }));

            // In JPush V3, Notification would not be display on screen, it would be transferred to app instead.// And different platform can provide different notification data.
            Notification notification = new Notification
            {
                AndroidNotification = new AndroidNotificationParameters
                {
                    Title = "JPush provides V3.",
                    Alert = "JPush V2 would be retired soon.",
                    CustomizedValues = customizedValues
                },
                iOSNotification = new iOSNotificationParameters
                {
                    Badge = 1,
                    Alert = "JPush V2 would be retired soon.",
                    Sound = "YourSound",
                    CustomizedValues = customizedValues
                }
            };

            var response = client.SendPushMessage(new PushMessageRequestV3
            {
                Audience = audience,
                Platform = PushPlatform.AndroidAndiOS,
                IsTestEnvironment = true,
                AppMessage = new AppMessage
                {
                    Content = "Hello, this is a test push of V3 from .NET. Have a nice day!",
                    CustomizedValue = customizedValues
                },
                Notification = notification
            });

            Console.WriteLine(response.ResponseCode.ToString() + ":"+ response.ResponseMessage);
            Console.WriteLine("Push sent.");
            Console.WriteLine(response.ResponseCode.ToString() + ":"+ response.ResponseMessage);


            List<string> idToCheck = new List<string>();
            idToCheck.Add(response.MessageId);
            var statusList = client.QueryPushMessageStatus(idToCheck);
            Console.WriteLine("Status track is completed.");
            if (statusList != null)
            {
                foreach (var one in statusList)
                {
                    Console.WriteLine(string.Format("Id: {0}, Android: {1}, iOS: {2}", one.MessageId, one.AndroidDeliveredCount, one.ApplePushNotificationDeliveredCount));
                }
            }
            Console.WriteLine("Press any key to exit.");
            Console.Read();
        }
    }
}

If you already use JPushClient V2 (Class: JPushClient), you can continue to use that. Library would convert the old PushMessageRequest to PushMessageRequestV3.

NOTE: the parameter in consturctor of JPushClient has lost effect. Whatever you set as true or false, SSL would be used according to JPush's new rule.

 

RESTful API reference: http://docs.jpush.cn/display/dev/Push-API-v3

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 v3 version of JPush.

Viewing all articles
Browse latest Browse all 21

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>