基线 SVN r240
SVN-Revision: r240
This commit is contained in:
@@ -0,0 +1,215 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Lskj.Push.Push.Audience;
|
||||
using System.Diagnostics;
|
||||
using Lskj.Push.Util;
|
||||
|
||||
namespace Lskj.Push.Push.Mode
|
||||
{
|
||||
public class Audience
|
||||
{
|
||||
private const String ALL = "all";
|
||||
public string allAudience;
|
||||
|
||||
private void AddWithAudienceTarget(AudienceTarget target)
|
||||
{
|
||||
Debug.Assert(target != null && target.valueBuilder != null);
|
||||
if (target != null && target.valueBuilder != null)
|
||||
{
|
||||
this.allAudience = null;
|
||||
if (dictionary == null)
|
||||
{
|
||||
dictionary = new Dictionary<string, HashSet<string>>();
|
||||
}
|
||||
if (dictionary.ContainsKey(target.audienceType.ToString()))
|
||||
{
|
||||
HashSet<string> origin = dictionary[target.audienceType.ToString()];
|
||||
foreach (var item in target.valueBuilder)
|
||||
{
|
||||
origin.Add(item);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
dictionary.Add(target.audienceType.ToString(), target.valueBuilder);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Dictionary<string, HashSet<string>> dictionary;
|
||||
private Audience()
|
||||
{
|
||||
allAudience = ALL;
|
||||
dictionary = null;
|
||||
}
|
||||
|
||||
public static Audience all()
|
||||
{
|
||||
return new Audience() { allAudience = ALL, dictionary = null }.Check();
|
||||
}
|
||||
public static Audience s_tag(HashSet<string> values)
|
||||
{
|
||||
return new Audience().tag(values);
|
||||
}
|
||||
public static Audience s_tag(params string[] values)
|
||||
{
|
||||
return new Audience().tag(values);
|
||||
}
|
||||
public static Audience s_tag_and(HashSet<string> values)
|
||||
{
|
||||
return new Audience().tag_and(values);
|
||||
}
|
||||
public static Audience s_tag_and(params string[] values)
|
||||
{
|
||||
return new Audience().tag_and(values);
|
||||
}
|
||||
public static Audience s_alias(HashSet<string> values)
|
||||
{
|
||||
return new Audience().alias(values);
|
||||
}
|
||||
public static Audience s_alias(params string[] values)
|
||||
{
|
||||
return new Audience().alias(values);
|
||||
}
|
||||
public static Audience s_segment(HashSet<string> values)
|
||||
{
|
||||
return new Audience().segment(values);
|
||||
}
|
||||
public static Audience s_segment(params string[] values)
|
||||
{
|
||||
return new Audience().segment(values);
|
||||
}
|
||||
public static Audience s_registrationId(HashSet<string> values)
|
||||
{
|
||||
return new Audience().registrationId(values);
|
||||
}
|
||||
public static Audience s_registrationId(params string[] values)
|
||||
{
|
||||
return new Audience().registrationId(values);
|
||||
}
|
||||
public Audience tag(HashSet<string> values)
|
||||
{
|
||||
if (allAudience != null)
|
||||
{
|
||||
allAudience = null;
|
||||
}
|
||||
AudienceTarget target = AudienceTarget.tag(values);
|
||||
AddWithAudienceTarget(target);
|
||||
return this.Check();
|
||||
}
|
||||
public Audience tag(params string[] values)
|
||||
{
|
||||
if (allAudience != null)
|
||||
{
|
||||
allAudience = null;
|
||||
}
|
||||
var valueList = new HashSet<string>(values);
|
||||
return tag(valueList);
|
||||
|
||||
}
|
||||
public Audience tag_and(HashSet<string> values)
|
||||
{
|
||||
if (allAudience != null)
|
||||
{
|
||||
allAudience = null;
|
||||
}
|
||||
AudienceTarget target = AudienceTarget.tag_and(values);
|
||||
this.allAudience = null;
|
||||
if (dictionary == null)
|
||||
{
|
||||
dictionary = new Dictionary<string, HashSet<string>>();
|
||||
}
|
||||
if (dictionary.ContainsKey(target.audienceType.ToString()))
|
||||
{
|
||||
HashSet<string> origin = dictionary[target.audienceType.ToString()];
|
||||
foreach (var item in values)
|
||||
{
|
||||
origin.Add(item);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
dictionary.Add(target.audienceType.ToString(), values);
|
||||
}
|
||||
return this.Check();
|
||||
}
|
||||
public Audience tag_and(params string[] values)
|
||||
{
|
||||
if (allAudience != null)
|
||||
{
|
||||
allAudience = null;
|
||||
}
|
||||
HashSet<string> list = new HashSet<string>(values);
|
||||
return tag_and(list);
|
||||
|
||||
}
|
||||
public Audience alias(HashSet<string> values)
|
||||
{
|
||||
if (allAudience != null)
|
||||
{
|
||||
allAudience = null;
|
||||
}
|
||||
AddWithAudienceTarget( AudienceTarget.alias(values));
|
||||
return this.Check();
|
||||
}
|
||||
public Audience alias(params string[] values)
|
||||
{
|
||||
if (allAudience != null)
|
||||
{
|
||||
allAudience = null;
|
||||
}
|
||||
return alias(new HashSet<string>(values));
|
||||
|
||||
}
|
||||
public Audience segment(HashSet<string> values)
|
||||
{
|
||||
if (allAudience != null)
|
||||
{
|
||||
allAudience = null;
|
||||
}
|
||||
AddWithAudienceTarget(AudienceTarget.segment(values));
|
||||
return this.Check();
|
||||
}
|
||||
public Audience segment(params string[] values)
|
||||
{
|
||||
if (allAudience != null)
|
||||
{
|
||||
allAudience = null;
|
||||
}
|
||||
return segment(new HashSet<string>(values));
|
||||
|
||||
}
|
||||
public Audience registrationId(HashSet<string> values)
|
||||
{
|
||||
if (allAudience != null)
|
||||
{
|
||||
allAudience = null;
|
||||
}
|
||||
AddWithAudienceTarget(AudienceTarget.registrationId(values));
|
||||
return this.Check();
|
||||
}
|
||||
public Audience registrationId(params string[] values)
|
||||
{
|
||||
if (allAudience != null)
|
||||
{
|
||||
allAudience = null;
|
||||
}
|
||||
return registrationId(new HashSet<string>(values));
|
||||
|
||||
}
|
||||
public bool isAll(){
|
||||
|
||||
return allAudience != null;
|
||||
}
|
||||
public Audience Check()
|
||||
{
|
||||
Preconditions.checkArgument(!(isAll() && null != dictionary), "Since all is enabled, any platform should not be set.");
|
||||
Preconditions.checkArgument(!(!isAll() && null == dictionary), "No any deviceType is set.");
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
using Lskj.Push.Util;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Lskj.Push.Push.Mode
|
||||
{
|
||||
public class Message
|
||||
{
|
||||
public String title{get;set;}
|
||||
public String msg_content { get; set; }
|
||||
public String content_type { get; set; }
|
||||
[JsonProperty]
|
||||
private Dictionary<string, object> extras { get; set; }
|
||||
|
||||
private Message()
|
||||
{
|
||||
|
||||
}
|
||||
private Message(String msgContent)
|
||||
{
|
||||
Preconditions.checkArgument(!(msgContent==null), "msgContent should be set");
|
||||
|
||||
this.title = null;
|
||||
this.msg_content = msgContent;
|
||||
this.content_type = null;
|
||||
this.extras = null;
|
||||
}
|
||||
private Message(String msgContent, String title, String contentType)
|
||||
{
|
||||
Preconditions.checkArgument(!(msgContent == null), "msgContent should be set");
|
||||
|
||||
this.title = title;
|
||||
this.msg_content = msgContent;
|
||||
this.content_type = contentType;
|
||||
}
|
||||
public static Message content(string msgContent)
|
||||
{
|
||||
return new Message(msgContent).Check();
|
||||
}
|
||||
public Message setTitle(String title)
|
||||
{
|
||||
this.title = title;
|
||||
return this;
|
||||
}
|
||||
public Message setContentType(String ContentType)
|
||||
{
|
||||
this.content_type = ContentType;
|
||||
return this;
|
||||
}
|
||||
public Message AddExtras(string key, string value)
|
||||
{
|
||||
if (extras == null)
|
||||
{
|
||||
extras = new Dictionary<string, object>();
|
||||
}
|
||||
if (value != null)
|
||||
{
|
||||
extras.Add(key, value);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
public Message AddExtras(string key, int value)
|
||||
{
|
||||
if (extras == null)
|
||||
{
|
||||
extras = new Dictionary<string, object>();
|
||||
}
|
||||
extras.Add(key, value);
|
||||
return this;
|
||||
}
|
||||
public Message AddExtras(string key, bool value)
|
||||
{
|
||||
if (extras == null)
|
||||
{
|
||||
extras = new Dictionary<string, object>();
|
||||
}
|
||||
extras.Add(key, value);
|
||||
return this;
|
||||
|
||||
}
|
||||
public Message AddExtras(string key, object value)
|
||||
{
|
||||
if (extras == null)
|
||||
{
|
||||
extras = new Dictionary<string, object>();
|
||||
}
|
||||
extras.Add(key, value);
|
||||
return this;
|
||||
|
||||
}
|
||||
public Message Check()
|
||||
{
|
||||
Preconditions.checkArgument(!(msg_content==null), "msgContent should be set");
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
using Lskj.Push.Push.Notification;
|
||||
using Lskj.Push.Util;
|
||||
using Newtonsoft.Json;
|
||||
// ------------------------------------------------------------------------------
|
||||
// <autogenerated>
|
||||
// This code was generated by a tool.
|
||||
// Mono Runtime Version: 4.0.30319.1
|
||||
//
|
||||
// Changes to this file may cause incorrect behavior and will be lost if
|
||||
// the code is regenerated.
|
||||
// </autogenerated>
|
||||
// ------------------------------------------------------------------------------
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
namespace Lskj.Push.Push.Mode
|
||||
{
|
||||
public class Notification
|
||||
{
|
||||
public String alert{get;set;}
|
||||
[JsonProperty(PropertyName = "ios")]
|
||||
public IosNotification IosNotification { get; set; }
|
||||
[JsonProperty(PropertyName = "android")]
|
||||
public AndroidNotification AndroidNotification { get; set; }
|
||||
[JsonProperty(PropertyName = "winphone")]
|
||||
public WinphoneNotification WinphoneNotification { get; set; }
|
||||
|
||||
public Notification()
|
||||
{
|
||||
this.alert = null;
|
||||
this.IosNotification = null;
|
||||
this.AndroidNotification = null;
|
||||
this.WinphoneNotification = null;
|
||||
}
|
||||
|
||||
public Notification setAlert(string alert)
|
||||
{
|
||||
this.alert = alert;
|
||||
return this;
|
||||
}
|
||||
public Notification setAndroid(AndroidNotification android)
|
||||
{
|
||||
this.AndroidNotification = android;
|
||||
return this;
|
||||
}
|
||||
public Notification setIos(IosNotification ios)
|
||||
{
|
||||
this.IosNotification = ios;
|
||||
return this;
|
||||
}
|
||||
public Notification setWinphone(WinphoneNotification winphone)
|
||||
{
|
||||
this.WinphoneNotification = winphone;
|
||||
return this;
|
||||
}
|
||||
|
||||
public static Notification android(String alert, String title)
|
||||
{
|
||||
var platformNotification = new AndroidNotification().setAlert(alert).setTitle(title);
|
||||
var notificaiton = new Notification().setAlert(alert);
|
||||
notificaiton.AndroidNotification = platformNotification;
|
||||
return notificaiton;
|
||||
}
|
||||
public static Notification ios(String alert)
|
||||
{
|
||||
var iosNotification = new IosNotification().setAlert(alert);
|
||||
var notification = new Notification().setAlert(alert);
|
||||
notification.IosNotification = iosNotification;
|
||||
return notification;
|
||||
}
|
||||
public static Notification ios_auto_badge()
|
||||
{
|
||||
var platformNotification = new IosNotification();
|
||||
platformNotification.autoBadge();
|
||||
var notificaiton = new Notification().setAlert("");;
|
||||
notificaiton.IosNotification = platformNotification;
|
||||
return notificaiton;
|
||||
}
|
||||
public static Notification ios_set_badge(int badge)
|
||||
{
|
||||
var platformNotification = new IosNotification();
|
||||
platformNotification.setBadge(badge);
|
||||
|
||||
var notificaiton = new Notification();
|
||||
notificaiton.IosNotification = platformNotification;
|
||||
return notificaiton;
|
||||
}
|
||||
public static Notification ios_incr_badge(int badge)
|
||||
{
|
||||
var platformNotification = new IosNotification();
|
||||
platformNotification.incrBadge(badge);
|
||||
var notificaiton = new Notification();
|
||||
notificaiton.IosNotification = platformNotification;
|
||||
return notificaiton;
|
||||
}
|
||||
public static Notification winphone(String alert)
|
||||
{
|
||||
var platformNotification = new WinphoneNotification().setAlert(alert);
|
||||
|
||||
var notificaiton = new Notification().setAlert(alert);
|
||||
notificaiton.WinphoneNotification = platformNotification;
|
||||
return notificaiton;
|
||||
}
|
||||
|
||||
public Notification Check()
|
||||
{
|
||||
Preconditions.checkArgument(!(isPlatformEmpty() && null == alert), "No notification payload is set.");
|
||||
if (IosNotification!=null)
|
||||
{
|
||||
Preconditions.checkArgument(!(null==IosNotification.alert && null == alert), "No notification payload is set.");
|
||||
}
|
||||
if(AndroidNotification!=null)
|
||||
{
|
||||
Preconditions.checkArgument(!(null == AndroidNotification.alert && null == alert), "No notification payload is set.");
|
||||
|
||||
}
|
||||
if (WinphoneNotification!=null)
|
||||
{
|
||||
Preconditions.checkArgument(!(null == WinphoneNotification.alert && null == alert), "No notification payload is set.");
|
||||
|
||||
}
|
||||
return this;
|
||||
}
|
||||
private bool isPlatformEmpty()
|
||||
{
|
||||
return (IosNotification == null && AndroidNotification == null && WinphoneNotification == null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
using Lskj.Push.Util;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Lskj.Push.Push.Mode
|
||||
{
|
||||
public class Options
|
||||
{
|
||||
private const long NONE_TIME_TO_LIVE = -1;
|
||||
|
||||
public Options()
|
||||
{
|
||||
this.sendno = 0;
|
||||
this.override_msg_id = 0;
|
||||
this.time_to_live = NONE_TIME_TO_LIVE;
|
||||
this.big_push_duration = 0;
|
||||
this.apns_production = false;
|
||||
}
|
||||
public Options(int sendno,
|
||||
long overrideMsgId,
|
||||
long timeToLive,
|
||||
int bigPushDuration,
|
||||
bool apnsProduction=false)
|
||||
{
|
||||
this.sendno = sendno;
|
||||
this.override_msg_id = overrideMsgId;
|
||||
this.time_to_live = timeToLive;
|
||||
this.big_push_duration = bigPushDuration;
|
||||
this.apns_production = apnsProduction;
|
||||
}
|
||||
private int _sendno;
|
||||
[DefaultValue(0)]
|
||||
public int sendno
|
||||
{
|
||||
get
|
||||
{
|
||||
return _sendno;
|
||||
}
|
||||
set
|
||||
{
|
||||
Preconditions.checkArgument(value >= 0, "sendno should be greater than 0.");
|
||||
_sendno = value;
|
||||
}
|
||||
}
|
||||
private long _override_msg_id;
|
||||
[DefaultValue(0)]
|
||||
public long override_msg_id
|
||||
{
|
||||
get
|
||||
{
|
||||
return _override_msg_id;
|
||||
}
|
||||
set
|
||||
{
|
||||
Preconditions.checkArgument(value >= 0, "override_msg_id should be greater than 0.");
|
||||
_override_msg_id = value;
|
||||
}
|
||||
}
|
||||
private long _time_to_live;
|
||||
[DefaultValue(NONE_TIME_TO_LIVE)]
|
||||
public long time_to_live
|
||||
{
|
||||
get
|
||||
{
|
||||
return _time_to_live;
|
||||
}
|
||||
set
|
||||
{
|
||||
Preconditions.checkArgument(value >= NONE_TIME_TO_LIVE, "time_to_live should be greater than 0.");
|
||||
_time_to_live = value;
|
||||
}
|
||||
}
|
||||
private long _big_push_duration;
|
||||
[DefaultValue(0)]
|
||||
public long big_push_duration
|
||||
{
|
||||
get
|
||||
{
|
||||
return _big_push_duration;
|
||||
}
|
||||
set
|
||||
{
|
||||
Preconditions.checkArgument(value >= 0, "big_push_duration should be greater than 0.");
|
||||
_big_push_duration = value;
|
||||
}
|
||||
}
|
||||
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Include)]
|
||||
public bool apns_production { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
using Lskj.Push.Common;
|
||||
using Lskj.Push.Util;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Lskj.Push.Push.Mode
|
||||
{
|
||||
public class Platform
|
||||
{
|
||||
private const String ALL = "all";
|
||||
[JsonProperty(PropertyName = "winphone")]
|
||||
public string allPlatform{get;set;}
|
||||
private HashSet<string> _deviceTypes;
|
||||
public HashSet<string> deviceTypes
|
||||
{
|
||||
get
|
||||
{
|
||||
return _deviceTypes;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value != null)
|
||||
{
|
||||
allPlatform = null;
|
||||
}
|
||||
_deviceTypes = value;
|
||||
}
|
||||
}
|
||||
private Platform()
|
||||
{
|
||||
allPlatform = ALL;
|
||||
deviceTypes = null;
|
||||
}
|
||||
private Platform(bool all, HashSet<string> deviceTypes)
|
||||
{
|
||||
//用来判断all=true时deviceTypes必须为空,反之当all=false时deviceTypes有值,不然json序列化会出错
|
||||
Debug.Assert(all && deviceTypes == null || !all && deviceTypes != null);
|
||||
if (all)
|
||||
{
|
||||
allPlatform = ALL;
|
||||
}
|
||||
this.deviceTypes = deviceTypes;
|
||||
}
|
||||
public static Platform all()
|
||||
{
|
||||
return new Platform(true, null).Check();
|
||||
}
|
||||
public static Platform ios()
|
||||
{
|
||||
HashSet<string> types = new HashSet<string>();
|
||||
types.Add(DeviceType.ios.ToString());
|
||||
return new Platform(false,types).Check();
|
||||
}
|
||||
public static Platform android()
|
||||
{
|
||||
HashSet<string> types = new HashSet<string>();
|
||||
types.Add(DeviceType.android.ToString());
|
||||
return new Platform(false, types).Check();
|
||||
}
|
||||
public static Platform winphone()
|
||||
{
|
||||
HashSet<string> types = new HashSet<string>();
|
||||
types.Add(DeviceType.winphone.ToString());
|
||||
return new Platform(false, types).Check();
|
||||
}
|
||||
public static Platform android_ios()
|
||||
{
|
||||
HashSet<string> types = new HashSet<string>();
|
||||
types.Add(DeviceType.android.ToString());
|
||||
types.Add(DeviceType.ios.ToString());
|
||||
return new Platform(false, types).Check();
|
||||
}
|
||||
public static Platform android_winphone()
|
||||
{
|
||||
HashSet<string> types = new HashSet<string>();
|
||||
types.Add(DeviceType.android.ToString());
|
||||
types.Add(DeviceType.winphone.ToString());
|
||||
return new Platform(false, types).Check();
|
||||
}
|
||||
public static Platform ios_winphone()
|
||||
{
|
||||
HashSet<string> types = new HashSet<string>();
|
||||
types.Add(DeviceType.ios.ToString());
|
||||
types.Add(DeviceType.winphone.ToString());
|
||||
|
||||
return new Platform(false, types).Check();
|
||||
}
|
||||
public bool isAll()
|
||||
{
|
||||
return allPlatform != null;
|
||||
}
|
||||
public void setAll(bool all)
|
||||
{
|
||||
if (all)
|
||||
{
|
||||
allPlatform = ALL;
|
||||
}
|
||||
else
|
||||
{
|
||||
allPlatform = null;
|
||||
}
|
||||
}
|
||||
public Platform Check()
|
||||
{
|
||||
Preconditions.checkArgument(!(isAll() && null != deviceTypes), "Since all is enabled, any platform should not be set.");
|
||||
Preconditions.checkArgument(!(!isAll() && null == deviceTypes), "No any deviceType is set.");
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
using Lskj.Push.Common;
|
||||
using Lskj.Push.Push.Notification;
|
||||
using Lskj.Push.Util;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
namespace Lskj.Push.Push.Mode
|
||||
{
|
||||
public class PushPayload
|
||||
{
|
||||
|
||||
private JsonSerializerSettings jSetting;
|
||||
private const String PLATFORM = "platform";
|
||||
private const String AUDIENCE = "audience";
|
||||
private const String NOTIFICATION = "notification";
|
||||
private const String MESSAGE = "message";
|
||||
private const String OPTIONS = "options";
|
||||
|
||||
private const int MAX_GLOBAL_ENTITY_LENGTH = 1200; // Definition acording to JPush Docs
|
||||
private const int MAX_IOS_PAYLOAD_LENGTH = 220; // Definition acording to JPush Docs
|
||||
|
||||
//serializaiton property
|
||||
[JsonConverter(typeof(PlatformConverter))]
|
||||
public Platform platform{get;set;}
|
||||
[JsonConverter(typeof(AudienceConverter))]
|
||||
public Audience audience{get;set;}
|
||||
public Notification notification { get; set; }
|
||||
public Message message { get; set; }
|
||||
public Options options { get; set; }
|
||||
//construct
|
||||
public PushPayload()
|
||||
{
|
||||
platform = null;
|
||||
audience = null;
|
||||
notification = null;
|
||||
message = null;
|
||||
options = new Options();
|
||||
jSetting = new JsonSerializerSettings();
|
||||
jSetting.NullValueHandling = NullValueHandling.Ignore;
|
||||
jSetting.DefaultValueHandling = DefaultValueHandling.Ignore;
|
||||
}
|
||||
public PushPayload(Platform platform, Audience audience, Notification notification, Message message = null, Options options = null)
|
||||
{
|
||||
Debug.Assert(platform != null);
|
||||
Debug.Assert(audience != null);
|
||||
Debug.Assert(notification != null || message != null);
|
||||
|
||||
this.platform = platform;
|
||||
this.audience = audience;
|
||||
this.notification = notification;
|
||||
this.message = message;
|
||||
this.options = options;
|
||||
|
||||
jSetting = new JsonSerializerSettings();
|
||||
jSetting.NullValueHandling = NullValueHandling.Ignore;
|
||||
jSetting.DefaultValueHandling = DefaultValueHandling.Ignore;
|
||||
}
|
||||
/**
|
||||
* The shortcut of building a simple alert notification object to all platforms and all audiences
|
||||
*/
|
||||
public static PushPayload AlertAll(String alert)
|
||||
{
|
||||
return new PushPayload(Platform.all(),
|
||||
Audience.all(),
|
||||
new Notification().setAlert(alert),
|
||||
null,
|
||||
new Options());
|
||||
}
|
||||
//* The shortcut of building a simple message object to all platforms and all audiences
|
||||
//*/
|
||||
public static PushPayload MessageAll(String msgContent)
|
||||
{
|
||||
return new PushPayload( Platform.all(),
|
||||
Audience.all(),
|
||||
null,
|
||||
Message.content(msgContent),
|
||||
new Options());
|
||||
}
|
||||
public static PushPayload FromJSON(String payloadString)
|
||||
{
|
||||
try
|
||||
{
|
||||
var jSetting = new JsonSerializerSettings();
|
||||
jSetting.NullValueHandling = NullValueHandling.Ignore;
|
||||
jSetting.DefaultValueHandling = DefaultValueHandling.Ignore;
|
||||
|
||||
var jsonObject = JsonConvert.DeserializeObject<PushPayload>(payloadString, jSetting);
|
||||
return jsonObject.Check();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine("JSON to PushPayLoad occur error:" + e.Message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public void ResetOptionsApnsProduction(bool apnsProduction)
|
||||
{
|
||||
if (this.options == null)
|
||||
{
|
||||
this.options = new Options();
|
||||
}
|
||||
this.options.apns_production = apnsProduction;
|
||||
}
|
||||
public void ResetOptionsTimeToLive(long timeToLive)
|
||||
{
|
||||
if (this.options == null)
|
||||
{
|
||||
this.options = new Options();
|
||||
}
|
||||
this.options.time_to_live = timeToLive;
|
||||
}
|
||||
public int GetSendno()
|
||||
{
|
||||
if (this.options != null)
|
||||
return this.options.sendno;
|
||||
return 0;
|
||||
}
|
||||
public bool IsGlobalExceedLength()
|
||||
{
|
||||
int messageLength = 0;
|
||||
if (message!= null)
|
||||
{
|
||||
var messageJson = JsonConvert.SerializeObject(this.message, jSetting);
|
||||
messageLength += UTF8Encoding.UTF8.GetBytes(messageJson).Length;
|
||||
}
|
||||
if (this.notification == null)
|
||||
{
|
||||
return messageLength > MAX_GLOBAL_ENTITY_LENGTH;
|
||||
}
|
||||
else
|
||||
{
|
||||
var notificationJson = JsonConvert.SerializeObject(this.notification);
|
||||
if (notificationJson != null)
|
||||
{
|
||||
messageLength += UTF8Encoding.UTF8.GetBytes(notificationJson).Length;
|
||||
}
|
||||
return messageLength > MAX_GLOBAL_ENTITY_LENGTH;
|
||||
}
|
||||
|
||||
}
|
||||
public bool IsIosExceedLength()
|
||||
{
|
||||
if (this.notification != null)
|
||||
{
|
||||
if (this.notification.IosNotification != null)
|
||||
{
|
||||
var iosJson = JsonConvert.SerializeObject(this.notification.IosNotification, jSetting);
|
||||
if (iosJson != null)
|
||||
{
|
||||
return UTF8Encoding.UTF8.GetBytes(iosJson).Length > MAX_IOS_PAYLOAD_LENGTH;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!(this.notification.alert==null))
|
||||
{
|
||||
string jsonText;
|
||||
using (StringWriter sw = new StringWriter())
|
||||
{
|
||||
JsonWriter writer = new JsonTextWriter(sw);
|
||||
writer.WriteValue(this.notification.alert);
|
||||
writer.Flush();
|
||||
jsonText = sw.GetStringBuilder().ToString();
|
||||
}
|
||||
return UTF8Encoding.UTF8.GetBytes(jsonText).Length > MAX_IOS_PAYLOAD_LENGTH;
|
||||
}
|
||||
else
|
||||
{
|
||||
// No iOS Payload
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public string ToJson()
|
||||
{
|
||||
return JsonConvert.SerializeObject(this, jSetting);
|
||||
}
|
||||
public PushPayload Check()
|
||||
{
|
||||
Preconditions.checkArgument(!(null == audience || null == platform), "audience and platform both should be set.");
|
||||
Preconditions.checkArgument(!(null == notification && null == message), "notification or message should be set at least one.");
|
||||
if (audience!=null)
|
||||
{
|
||||
audience.Check();
|
||||
}
|
||||
if (platform != null)
|
||||
{
|
||||
platform.Check();
|
||||
}
|
||||
if (message != null)
|
||||
{
|
||||
message.Check();
|
||||
}
|
||||
if (notification != null)
|
||||
{
|
||||
notification.Check();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Lskj.Push.Push.Mode
|
||||
{
|
||||
public class PushUserObj
|
||||
{
|
||||
public string username { get; set; }
|
||||
public string password { get; set; }
|
||||
public string nickname { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user