using System; using System.Collections.Generic; using System.Linq; using System.Web; using Newtonsoft.Json; namespace Types.DTO { public class Notification { public string title { get; set; } public string body { get; set; } } public class Message { /// /// Gets or sets the notification information to be included in the message. /// [JsonProperty("notification")] public Notification Notification { get; set; } /// /// Gets or sets the registration token of the device to which the message should be sent. /// [JsonProperty("token")] public string Token { get; set; } /// /// Gets or sets the name of the FCM topic to which the message should be sent. Topic names /// may contain the /topics/ prefix. /// public string topic { get; set; } /// /// Gets or sets the Android-specific information to be included in the message. /// [JsonProperty("android")] public AndroidConfig Android { get; set; } } public sealed class AndroidConfig { /// /// Gets or sets a collapse key for the message. Collapse key serves as an identifier for a /// group of messages that can be collapsed, so that only the last message gets sent when /// delivery can be resumed. A maximum of 4 different collapse keys may be active at any /// given time. /// [JsonProperty("collapse_key")] public string CollapseKey { get; set; } /// /// Gets or sets the priority of the message. /// [JsonIgnore] public Priority? Priority { get; set; } /// /// Gets or sets the time-to-live duration of the message. /// [JsonIgnore] public TimeSpan? TimeToLive { get; set; } /// /// Gets or sets the package name of the application where the registration tokens must /// match in order to receive the message. /// [JsonProperty("restricted_package_name")] public string RestrictedPackageName { get; set; } /// /// Gets or sets a collection of key-value pairs that will be added to the message as data /// fields. Keys and the values must not be null. When set, overrides any data fields set /// on the top-level /// . /// [JsonProperty("data")] public IReadOnlyDictionary Data { get; set; } /// /// Gets or sets the string representation of as accepted by the FCM /// backend service. /// [JsonProperty("priority")] private string PriorityString { get { switch (this.Priority) { case DTO.Priority.High: return "high"; case DTO.Priority.Normal: return "normal"; default: return null; } } set { switch (value) { case "high": this.Priority = DTO.Priority.High; return; case "normal": this.Priority = DTO.Priority.High; return; default: throw new ArgumentException( $"Invalid priority value: {value}. Only 'high' and 'normal'" + " are allowed."); } } } /// /// Gets or sets the string representation of as accepted by the /// FCM backend service. The string ends in the suffix "s" (indicating seconds) and is /// preceded by the number of seconds, with nanoseconds expressed as fractional seconds. /// [JsonProperty("ttl")] private string TtlString { get { if (this.TimeToLive == null) { return null; } var totalSeconds = this.TimeToLive.Value.TotalSeconds; var seconds = (long)Math.Floor(totalSeconds); var subsecondNanos = (long)((totalSeconds - seconds) * 1e9); if (subsecondNanos > 0) { return string.Format("{0}.{1:D9}s", seconds, subsecondNanos); } return string.Format("{0}s", seconds); } set { var segments = value.TrimEnd('s').Split('.'); var seconds = long.Parse(segments[0]); var ttl = TimeSpan.FromSeconds(seconds); if (segments.Length == 2) { var subsecondNanos = long.Parse(segments[1].TrimStart('0')); ttl = ttl.Add(TimeSpan.FromMilliseconds(subsecondNanos / 1e6)); } this.TimeToLive = ttl; } } } public class MessageMain { [JsonProperty("message")] public Message message { get; set; } } /// /// Priority levels that can be set on an . /// public enum Priority { /// /// High priority message. /// High, /// /// Normal priority message. /// Normal, } }