Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PushNotificationPayload.cs
1using System;
5
7{
12 {
16 public const string DefaultKind = "PushNotificationPayload";
17
21 public const int CurrentVersion = 1;
22
26 public string PayloadKind { get; set; } = DefaultKind;
27
31 public int PayloadVersion { get; set; } = CurrentVersion;
32
36 public PushVisual Visual { get; set; } = new PushVisual();
37
41 public PushAction Action { get; set; }
42
46 public PushChannel Channel { get; set; }
47
51 public PushDelivery Delivery { get; set; } = new PushDelivery();
52
56 public Dictionary<string, object> Context { get; set; }
57
61 public Dictionary<ClientType, Dictionary<string, object>> PlatformOverrides { get; set; }
62
66 public Dictionary<string, object> CustomData { get; set; }
67
71 public static bool TryParse(object Content, out PushNotificationPayload Payload)
72 {
73 object Normalized = Normalize(Content);
74
75 if (Normalized is PushNotificationPayload Parsed && Parsed.IsPayloadKind())
76 {
77 Payload = Parsed;
78 return true;
79 }
80
81 if (Normalized is Dictionary<string, object> Dict && HasPayloadMarker(Dict))
82 {
83 Payload = FromDictionary(Dict);
84 return true;
85 }
86
87 Payload = null;
88 return false;
89 }
90
91 private static PushNotificationPayload FromDictionary(Dictionary<string, object> Dict)
92 {
94 {
95 PayloadKind = GetString(Dict, "payloadKind") ?? GetString(Dict, "PayloadKind") ?? DefaultKind,
96 PayloadVersion = GetInt(Dict, "payloadVersion") ?? GetInt(Dict, "PayloadVersion") ?? CurrentVersion,
97 Visual = PushVisual.FromDictionary(GetDict(Dict, "visual")),
98 Action = PushAction.FromDictionary(GetDict(Dict, "action")),
99 Channel = PushChannel.FromDictionary(GetDict(Dict, "channel")),
100 Delivery = PushDelivery.FromDictionary(GetDict(Dict, "delivery")),
101 Context = GetDict(Dict, "context"),
102 CustomData = GetDict(Dict, "data") ?? GetDict(Dict, "customData") ?? GetDict(Dict, "custom"),
103 PlatformOverrides = ParsePlatformOverrides(GetDict(Dict, "platformOverrides"))
104 };
105
106 return Payload;
107 }
108
109 private static Dictionary<ClientType, Dictionary<string, object>> ParsePlatformOverrides(Dictionary<string, object> Dict)
110 {
111 if (Dict is null || Dict.Count == 0)
112 return null;
113
114 Dictionary<ClientType, Dictionary<string, object>> Result = new Dictionary<ClientType, Dictionary<string, object>>();
115
116 foreach (KeyValuePair<string, object> P in Dict)
117 {
118 if (!Enum.TryParse(P.Key, true, out ClientType ClientType))
119 continue;
120
121 if (Normalize(P.Value) is Dictionary<string, object> OverrideDict && OverrideDict.Count > 0)
122 Result[ClientType] = OverrideDict;
123 }
124
125 return Result.Count == 0 ? null : Result;
126 }
127
128 private static bool HasPayloadMarker(Dictionary<string, object> Dict)
129 {
130 if (Dict is null)
131 return false;
132
133 string PayloadKind = GetString(Dict, "payloadKind") ?? GetString(Dict, "PayloadKind");
134 return !string.IsNullOrEmpty(PayloadKind) && string.Equals(PayloadKind, DefaultKind, StringComparison.OrdinalIgnoreCase);
135 }
136
137 private bool IsPayloadKind()
138 {
139 return !string.IsNullOrEmpty(this.PayloadKind) &&
140 string.Equals(this.PayloadKind, DefaultKind, StringComparison.OrdinalIgnoreCase);
141 }
142
143 private static object Normalize(object Value)
144 {
145 if (Value is IElement Element)
147
148 if (Value is Dictionary<string, IElement> ScriptDict)
149 {
150 Dictionary<string, object> Converted = new Dictionary<string, object>();
151 foreach (KeyValuePair<string, IElement> P in ScriptDict)
152 Converted[P.Key] = Normalize(P.Value);
153
154 return Converted;
155 }
156
157 if (Value is Dictionary<string, object> Dict)
158 {
159 Dictionary<string, object> Converted = new Dictionary<string, object>();
160 foreach (KeyValuePair<string, object> P in Dict)
161 Converted[P.Key] = Normalize(P.Value);
162
163 return Converted;
164 }
165
166 if (Value is IEnumerable Enumerable && !(Value is string))
167 {
168 List<object> Items = new List<object>();
169 foreach (object Item in Enumerable)
170 Items.Add(Normalize(Item));
171
172 return Items.ToArray();
173 }
174
175 return Value;
176 }
177
178 internal static Dictionary<string, object> GetDict(Dictionary<string, object> Parent, string Key)
179 {
180 if (Parent is null)
181 return null;
182
183 if (!Parent.TryGetValue(Key, out object Value))
184 return null;
185
186 return Normalize(Value) as Dictionary<string, object>;
187 }
188
189 internal static string GetString(Dictionary<string, object> Dict, string Key)
190 {
191 if (Dict is null)
192 return null;
193
194 if (!Dict.TryGetValue(Key, out object Value) || Value is null)
195 return null;
196
197 return Value.ToString();
198 }
199
200 internal static int? GetInt(Dictionary<string, object> Dict, string Key)
201 {
202 if (Dict is null || !Dict.TryGetValue(Key, out object Value) || Value is null)
203 return null;
204
205 switch (Value)
206 {
207 case int i:
208 return i;
209 case long l:
210 return (int)l;
211 case double d:
212 return (int)d;
213 default:
214 if (int.TryParse(Value.ToString(), out int i2))
215 return i2;
216 return null;
217 }
218 }
219
220 internal static bool? GetBool(Dictionary<string, object> Dict, string Key)
221 {
222 if (Dict is null || !Dict.TryGetValue(Key, out object Value) || Value is null)
223 return null;
224
225 switch (Value)
226 {
227 case bool b:
228 return b;
229 default:
230 if (bool.TryParse(Value.ToString(), out bool b2))
231 return b2;
232 return null;
233 }
234 }
235 }
236
240 public class PushVisual
241 {
243 public string Title { get; set; }
245 public string Subtitle { get; set; }
247 public string Body { get; set; }
249 public string Icon { get; set; }
251 public string Badge { get; set; }
252
253 internal static PushVisual FromDictionary(Dictionary<string, object> Dict)
254 {
255 if (Dict is null)
256 return new PushVisual();
257
258 return new PushVisual
259 {
260 Title = PushNotificationPayload.GetString(Dict, "title"),
261 Subtitle = PushNotificationPayload.GetString(Dict, "subtitle"),
262 Body = PushNotificationPayload.GetString(Dict, "body"),
263 Icon = PushNotificationPayload.GetString(Dict, "icon"),
264 Badge = PushNotificationPayload.GetString(Dict, "badge"),
265 };
266 }
267 }
268
272 public class PushAction
273 {
275 public string Type { get; set; }
277 public string EntityId { get; set; }
279 public string CorrelationId { get; set; }
281 public string NavigationTarget { get; set; }
282
283 internal static PushAction FromDictionary(Dictionary<string, object> Dict)
284 {
285 if (Dict is null)
286 return null;
287
288 return new PushAction
289 {
290 Type = PushNotificationPayload.GetString(Dict, "type"),
291 EntityId = PushNotificationPayload.GetString(Dict, "entityId"),
292 CorrelationId = PushNotificationPayload.GetString(Dict, "correlationId"),
293 NavigationTarget = PushNotificationPayload.GetString(Dict, "navigationTarget")
294 };
295 }
296 }
297
301 public class PushChannel
302 {
304 public string ChannelId { get; set; }
306 public string Category { get; set; }
308 public string Importance { get; set; }
310 public string[] Tags { get; set; }
311
312 internal static PushChannel FromDictionary(Dictionary<string, object> Dict)
313 {
314 if (Dict is null)
315 return null;
316
317 object TagsValue = null;
318 Dict?.TryGetValue("tags", out TagsValue);
319
320 string[] Tags = TagsValue switch
321 {
322 string[] Arr => Arr,
323 IEnumerable<object> Items => new List<string>(Array.ConvertAll(new List<object>(Items).ToArray(), x => x.ToString())).ToArray(),
324 _ => null
325 };
326
327 return new PushChannel
328 {
329 ChannelId = PushNotificationPayload.GetString(Dict, "channelId") ?? PushNotificationPayload.GetString(Dict, "channel_id"),
330 Category = PushNotificationPayload.GetString(Dict, "category"),
331 Importance = PushNotificationPayload.GetString(Dict, "importance"),
332 Tags = Tags
333 };
334 }
335 }
336
340 public class PushDelivery
341 {
343 public string Priority { get; set; }
345 public int? Ttl { get; set; }
347 public string CollapseKey { get; set; }
349 public bool Silent { get; set; }
350
351 internal static PushDelivery FromDictionary(Dictionary<string, object> Dict)
352 {
353 if (Dict is null)
354 return new PushDelivery();
355
356 return new PushDelivery
357 {
358 Priority = PushNotificationPayload.GetString(Dict, "priority"),
359 Ttl = PushNotificationPayload.GetInt(Dict, "ttl"),
360 CollapseKey = PushNotificationPayload.GetString(Dict, "collapseKey") ?? PushNotificationPayload.GetString(Dict, "collapse_key"),
361 Silent = PushNotificationPayload.GetBool(Dict, "silent") ?? false
362 };
363 }
364 }
365}
string Type
Action type identifier (e.g. OpenChat, OpenContract).
string[] Tags
Tags to help clients route notifications locally.
Transport-agnostic push notification payload describing notification intent.
PushChannel Channel
Client-side channel/category information.
Dictionary< string, object > Context
Arbitrary context metadata.
Dictionary< string, object > CustomData
Additional custom data to include in the notification payload.
static bool TryParse(object Content, out PushNotificationPayload Payload)
Attempts to parse a payload from a script or serialized object.
PushDelivery Delivery
Delivery characteristics (priority, TTL, collapse key, silent).
PushAction Action
Action to execute when the notification is acted on.
Dictionary< ClientType, Dictionary< string, object > > PlatformOverrides
Platform-specific override fragments keyed by client type.
Base class for all types of elements.
Definition: Element.cs:14
abstract object AssociatedObjectValue
Associated object value.
Definition: Element.cs:43
Basic interface for all types of elements.
Definition: IElement.cs:21
Definition: ImplTypes.g.cs:58
ClientType
Type of client requesting notification.
Definition: ClientType.cs:7