Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
NotificationMessageApple.cs
1using System;
3
5{
12 {
17 {
18 }
19
25 public string Sound { get; set; }
26
34 public string Badge { get; set; }
35
39 public string SubTitle { get; set; }
40
48 public string BodyLocalizationKey { get; set; }
49
57 public string[] BodyLocalizationArguments { get; set; }
58
66 public string TitleLocalizationKey { get; set; }
67
75 public string[] TitleLocalizationArguments { get; set; }
76
80 public object AppleSpecificDeliveryOptions { get; set; }
81
85 public bool ContentAvailable { get; set; }
86
90 public bool MutableContent { get; set; }
91
96 public override void ExportProperties(Dictionary<string, object> Message)
97 {
98 base.ExportProperties(Message);
99
100 Dictionary<string, object> ApnsConfig = Message.ContainsKey("apns") && Message["apns"] is Dictionary<string, object> ExistingApns
101 ? ExistingApns
102 : new Dictionary<string, object>();
103 Dictionary<string, object> Payload = ApnsConfig.ContainsKey("payload") && ApnsConfig["payload"] is Dictionary<string, object> ExistingPayload
104 ? ExistingPayload
105 : new Dictionary<string, object>();
106 Dictionary<string, object> Aps = Payload.ContainsKey("aps") && Payload["aps"] is Dictionary<string, object> ExistingAps
107 ? ExistingAps
108 : new Dictionary<string, object>();
109 Dictionary<string, object> Headers = ApnsConfig.ContainsKey("headers") && ApnsConfig["headers"] is Dictionary<string, object> ExistingHeaders
110 ? ExistingHeaders
111 : new Dictionary<string, object>();
112 Dictionary<string, object> Alert = new Dictionary<string, object>();
113
114 if (this.SuppressNotification)
115 this.ContentAvailable = true;
116
117 NotificationPriority EffectivePriority = this.SuppressNotification ? NotificationPriority.Normal : this.Priority;
118
119 // Map priority: high = 10, normal = 5
120 Headers["apns-priority"] = ((int)EffectivePriority).ToString();
121
122 // Map TTL to apns-expiration (as Unix timestamp)
123 if (this.TimeToLive > 0)
124 {
125 long ExpirationTime = DateTimeOffset.UtcNow.AddSeconds(this.TimeToLive).ToUnixTimeSeconds();
126 Headers["apns-expiration"] = ExpirationTime.ToString();
127 }
128
129 if (!this.SuppressNotification)
130 {
131 if (!string.IsNullOrEmpty(this.Title))
132 Alert["title"] = this.Title;
133
134 if (!string.IsNullOrEmpty(this.Body))
135 Alert["body"] = this.Body;
136
137 if (!string.IsNullOrEmpty(this.SubTitle))
138 Alert["subtitle"] = this.SubTitle;
139 }
140
141 // Add notification properties
142 if (!string.IsNullOrEmpty(this.Sound) && !this.SuppressNotification)
143 Aps["sound"] = this.Sound;
144
145 if (!string.IsNullOrEmpty(this.Badge) && !this.SuppressNotification)
146 Aps["badge"] = this.Badge;
147
148 if (!string.IsNullOrEmpty(this.BodyLocalizationKey))
149 {
150 Aps["body_loc_key"] = this.BodyLocalizationKey;
151
152 if (!(this.BodyLocalizationArguments is null))
153 Aps["body_loc_args"] = this.BodyLocalizationArguments;
154 }
155
156 if (!string.IsNullOrEmpty(this.TitleLocalizationKey))
157 {
158 Aps["title_loc_key"] = this.TitleLocalizationKey;
159
160 if (!(this.TitleLocalizationArguments is null))
161 Aps["title_loc_args"] = this.TitleLocalizationArguments;
162 }
163
164 if (this.ContentAvailable)
165 Aps["content-available"] = 1;
166
167 if (this.MutableContent)
168 Aps["mutable-content"] = 1;
169
170 if (Alert.Count > 0)
171 Aps["alert"] = Alert;
172
173 if (Aps.Count > 0)
174 Payload["aps"] = Aps;
175
176 if (Payload.Count > 0)
177 ApnsConfig["payload"] = Payload;
178
179 // Set apns-push-type automatically
180 if (Headers.Count > 0)
181 ApnsConfig["headers"] = Headers;
182
183 if (!ApnsConfig.TryGetValue("headers", out object HeadersObj) || !(HeadersObj is Dictionary<string, object> HeadersFinal))
184 {
185 HeadersFinal = Headers.Count > 0 ? new Dictionary<string, object>(Headers) : new Dictionary<string, object>();
186 ApnsConfig["headers"] = HeadersFinal;
187 }
188
189 string PushType = (Alert.Count > 0 || !string.IsNullOrEmpty(this.Sound) || !string.IsNullOrEmpty(this.Badge)) ? "alert" : "background";
190 HeadersFinal["apns-push-type"] = PushType;
191
192 if (Headers.Count > 0)
193 ApnsConfig["headers"] = Headers;
194
195 if (this.AppleSpecificDeliveryOptions is Dictionary<string, object> ApnsConfig2)
196 {
197 foreach (KeyValuePair<string, object> P in ApnsConfig2)
198 {
199 if (ApnsConfig.TryGetValue(P.Key, out object Obj) &&
200 Obj is Dictionary<string, object> Obj1 &&
201 P.Value is Dictionary<string, object> Obj2)
202 {
203 foreach (KeyValuePair<string, object> P2 in Obj2)
204 {
205 if (Obj1.TryGetValue(P2.Key, out object Obj3) &&
206 Obj3 is Dictionary<string, object> Obj31 &&
207 P2.Value is Dictionary<string, object> Obj32)
208 {
209 foreach (KeyValuePair<string, object> P3 in Obj32)
210 {
211 Obj31[P3.Key] = P3.Value;
212 }
213 }
214 else
215 Obj1[P2.Key] = P2.Value;
216 }
217 }
218 else
219 ApnsConfig[P.Key] = P.Value;
220 }
221 }
222
223 if (ApnsConfig.Count > 0)
224 Message["apns"] = ApnsConfig;
225 }
226
233 public override bool TrySetProperty(string Name, object Value)
234 {
235 switch (Name)
236 {
237 case "apns-push-type":
238 this.AddHeader("apns-push-type", Value);
239 return true;
240
241 case "apns-topic":
242 this.AddHeader("apns-topic", Value);
243 return true;
244
245 case "sound":
246 this.Sound = Value?.ToString();
247 return true;
248
249 case "badge":
250 this.Badge = Value?.ToString();
251 return true;
252
253 case "subtitle":
254 this.SubTitle = Value?.ToString();
255 return true;
256
257 case "body_loc_key":
258 this.BodyLocalizationKey = Value?.ToString();
259 return true;
260
261 case "body_loc_args":
262 if (Value is string[] BodyLocalizationArguments)
263 {
264 this.BodyLocalizationArguments = BodyLocalizationArguments;
265 return true;
266 }
267 break;
268
269 case "title_loc_key":
270 this.TitleLocalizationKey = Value?.ToString();
271 return true;
272
273 case "title_loc_args":
274 if (Value is string[] TitleLocalizationArguments)
275 {
276 this.TitleLocalizationArguments = TitleLocalizationArguments;
277 return true;
278 }
279 break;
280
281 case "content_available":
282 if (Value is bool ContentAvailable)
283 {
284 this.ContentAvailable = ContentAvailable;
285 return true;
286 }
287 break;
288
289 case "mutable_content":
290 if (Value is bool MutableContent)
291 {
292 this.MutableContent = MutableContent;
293 return true;
294 }
295 break;
296
297 case "apns":
298 this.AppleSpecificDeliveryOptions = Value;
299 break;
300 }
301
302 return base.TrySetProperty(Name, Value);
303 }
304
305 private void AddHeader(string Name, object Value)
306 {
307 if (Value is null)
308 return;
309
310 if (!(this.AppleSpecificDeliveryOptions is Dictionary<string, object> Options))
311 {
312 Options = new Dictionary<string, object>();
313 this.AppleSpecificDeliveryOptions = Options;
314 }
315
316 if (!Options.TryGetValue("headers", out object HeadersObj) || !(HeadersObj is Dictionary<string, object> Headers))
317 {
318 Headers = new Dictionary<string, object>();
319 Options["headers"] = Headers;
320 }
321
322 Headers[Name] = Value.ToString();
323 }
324
325 }
326}
string Sound
The sound to play when the device receives the notification.
string[] BodyLocalizationArguments
Variable string values to be used in place of the format specifiers in body_loc_key to use to localiz...
object AppleSpecificDeliveryOptions
Apple-specific delivery options.
string Badge
The value of the badge on the home screen app icon.
bool ContentAvailable
On Apple platforms, this field indicates if the notification should wake an inactive client app (cont...
string BodyLocalizationKey
The key to the body string in the app's string resources to use to localize the body text to the user...
override void ExportProperties(Dictionary< string, object > Message)
Prepares the object to send to Firebase.
string TitleLocalizationKey
The key to the title string in the app's string resources to use to localize the title text to the us...
string[] TitleLocalizationArguments
Variable string values to be used in place of the format specifiers in title_loc_key to use to locali...
bool MutableContent
On Apple platforms, this indicates if notification content can be modified before display (mutable-co...
override bool TrySetProperty(string Name, object Value)
Tries to set a property value.
bool SuppressNotification
If the visual notification part should be suppressed (data-only/background).
int TimeToLive
This parameter specifies how long (in seconds) the message should be kept in FCM storage if the devic...
NotificationPriority Priority
Sets the priority of the message. Valid values are "normal" and "high."
NotificationPriority
Notification priority