Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PushNotificationClient.cs
1using System;
2using System.Text;
3using System.Threading.Tasks;
5using Waher.Events;
7
9{
14 {
18 public const string MessagePushNamespace = "http://waher.se/Schema/PushNotification.xsd";
19
25 : base(Client)
26 {
27 }
28
32 public override string[] Extensions => Array.Empty<string>();
33
34 #region New Token
35
44 public Task NewToken(string Token, PushMessagingService Service, ClientType ClientType, EventHandlerAsync<IqResultEventArgs> Callback, object State)
45 {
46 StringBuilder Xml = new StringBuilder();
47
48 Xml.Append("<newToken xmlns='");
49 Xml.Append(MessagePushNamespace);
50 Xml.Append("' service='");
51 Xml.Append(Service.ToString());
52 Xml.Append("' clientType='");
53 Xml.Append(ClientType.ToString());
54 Xml.Append("' token='");
55 Xml.Append(XML.Encode(Token));
56 Xml.Append("'/>");
57
58 return this.client.SendIqSet(string.Empty, Xml.ToString(), Callback, State);
59 }
60
67 public async Task NewTokenAsync(string Token, PushMessagingService Service, ClientType ClientType)
68 {
69 TaskCompletionSource<bool> Result = new TaskCompletionSource<bool>();
70
71 await this.NewToken(Token, Service, ClientType, (Sender, e) =>
72 {
73 if (e.Ok)
74 Result.TrySetResult(true);
75 else
76 Result.TrySetException(e.StanzaError ?? new Exception("Unable to report new token."));
77
78 return Task.CompletedTask;
79 }, null);
80
81 await Result.Task;
82 }
83
84 #endregion
85
86 #region Remove Token
87
93 public Task RemoveToken(EventHandlerAsync<IqResultEventArgs> Callback, object State)
94 {
95 StringBuilder Xml = new StringBuilder();
96
97 Xml.Append("<removeToken xmlns='");
98 Xml.Append(MessagePushNamespace);
99 Xml.Append("'/>");
100
101 return this.client.SendIqSet(string.Empty, Xml.ToString(), Callback, State);
102 }
103
107 public async Task RemoveTokenAsync()
108 {
109 TaskCompletionSource<bool> Result = new TaskCompletionSource<bool>();
110
111 await this.RemoveToken((Sender, e) =>
112 {
113 if (e.Ok)
114 Result.TrySetResult(true);
115 else
116 Result.TrySetException(e.StanzaError ?? new Exception("Unable to remove token."));
117
118 return Task.CompletedTask;
119 }, null);
120
121 await Result.Task;
122 }
123
124 #endregion
125
126 #region Clear Rules
127
133 public Task ClearRules(EventHandlerAsync<IqResultEventArgs> Callback, object State)
134 {
135 StringBuilder Xml = new StringBuilder();
136
137 Xml.Append("<clearRules xmlns='");
138 Xml.Append(MessagePushNamespace);
139 Xml.Append("'/>");
140
141 return this.client.SendIqSet(string.Empty, Xml.ToString(), Callback, State);
142 }
143
147 public async Task ClearRulesAsync()
148 {
149 TaskCompletionSource<bool> Result = new TaskCompletionSource<bool>();
150
151 await this.ClearRules((Sender, e) =>
152 {
153 if (e.Ok)
154 Result.TrySetResult(true);
155 else
156 Result.TrySetException(e.StanzaError ?? new Exception("Unable to clear available rules."));
157
158 return Task.CompletedTask;
159 }, null);
160
161 await Result.Task;
162 }
163
164 #endregion
165
166 #region Add Push Notification Rule
167
189 public Task AddRule(MessageType MessageType, string LocalName, string Namespace, string Channel, string MessageVariable,
190 string PatternMatchingScript, string ContentScript, EventHandlerAsync<IqResultEventArgs> Callback, object State)
191 {
192 StringBuilder Xml = new StringBuilder();
193
194 Xml.Append("<addRule xmlns='");
195 Xml.Append(MessagePushNamespace);
196 Xml.Append("' type='");
197
198 if (MessageType != MessageType.Normal)
199 Xml.Append(MessageType.ToString().ToLower());
200
201 Xml.Append("' localName='");
202 Xml.Append(XML.Encode(LocalName));
203 Xml.Append("' namespace='");
204 Xml.Append(XML.Encode(Namespace));
205 Xml.Append("' channel='");
206 Xml.Append(XML.Encode(Channel));
207 Xml.Append("' variable='");
208 Xml.Append(XML.Encode(MessageVariable));
209 Xml.Append("'>");
210
211 if (!string.IsNullOrEmpty(PatternMatchingScript))
212 {
213 Xml.Append("<PatternMatching>");
214 Xml.Append(XML.Encode(PatternMatchingScript));
215 Xml.Append("</PatternMatching>");
216 }
217
218 if (!string.IsNullOrEmpty(ContentScript))
219 {
220 Xml.Append("<Content>");
221 Xml.Append(XML.Encode(ContentScript));
222 Xml.Append("</Content>");
223 }
224
225 Xml.Append("</addRule>");
226
227 return this.client.SendIqSet(string.Empty, Xml.ToString(), Callback, State);
228 }
229
249 public async Task AddRuleAsync(MessageType MessageType, string LocalName, string Namespace, string Channel, string MessageVariable,
250 string PatternMatchingScript, string ContentScript)
251 {
252 TaskCompletionSource<bool> Result = new TaskCompletionSource<bool>();
253
254 await this.AddRule(MessageType, LocalName, Namespace, Channel, MessageVariable, PatternMatchingScript, ContentScript, (Sender, e) =>
255 {
256 if (e.Ok)
257 Result.TrySetResult(true);
258 else
259 Result.TrySetException(e.StanzaError ?? new Exception("Unable to add push notification rule."));
260
261 return Task.CompletedTask;
262 }, null);
263
264 await Result.Task;
265 }
266
267 #endregion
268
269 #region Remove Push Notification Rule
270
279 public Task RemoveRule(MessageType MessageType, string LocalName, string Namespace, EventHandlerAsync<IqResultEventArgs> Callback, object State)
280 {
281 StringBuilder Xml = new StringBuilder();
282
283 Xml.Append("<removeRule xmlns='");
284 Xml.Append(MessagePushNamespace);
285 Xml.Append("' type='");
286
287 if (MessageType != MessageType.Normal)
288 Xml.Append(MessageType.ToString().ToLower());
289
290 Xml.Append("' localName='");
291 Xml.Append(XML.Encode(LocalName));
292 Xml.Append("' namespace='");
293 Xml.Append(XML.Encode(Namespace));
294 Xml.Append("'/>");
295
296 return this.client.SendIqSet(string.Empty, Xml.ToString(), Callback, State);
297 }
298
305 public async Task RemoveRuleAsync(MessageType MessageType, string LocalName, string Namespace)
306 {
307 TaskCompletionSource<bool> Result = new TaskCompletionSource<bool>();
308
309 await this.RemoveRule(MessageType, LocalName, Namespace, (Sender, e) =>
310 {
311 if (e.Ok)
312 Result.TrySetResult(true);
313 else
314 Result.TrySetException(e.StanzaError ?? new Exception("Unable to remove push notification rule."));
315
316 return Task.CompletedTask;
317 }, null);
318
319 await Result.Task;
320 }
321
322 #endregion
323
324 }
325}
Helps with common XML-related tasks.
Definition: XML.cs:21
static string Encode(string s)
Encodes a string for use in XML.
Definition: XML.cs:29
async Task NewTokenAsync(string Token, PushMessagingService Service, ClientType ClientType)
Reports a new push token to the server.
Task RemoveRule(MessageType MessageType, string LocalName, string Namespace, EventHandlerAsync< IqResultEventArgs > Callback, object State)
Removes a push notification rule from the client account.
override string[] Extensions
Implemented extensions.
PushNotificationClient(XmppClient Client)
Push Notification Client
Task ClearRules(EventHandlerAsync< IqResultEventArgs > Callback, object State)
Clears available push notification rules for the client.
Task AddRule(MessageType MessageType, string LocalName, string Namespace, string Channel, string MessageVariable, string PatternMatchingScript, string ContentScript, EventHandlerAsync< IqResultEventArgs > Callback, object State)
Adds a push notification rule to the client account.
const string MessagePushNamespace
http://waher.se/Schema/PushNotification.xsd
async Task RemoveTokenAsync()
Removes the last push token from the server.
Task RemoveToken(EventHandlerAsync< IqResultEventArgs > Callback, object State)
Removes the last push token from the server.
async Task AddRuleAsync(MessageType MessageType, string LocalName, string Namespace, string Channel, string MessageVariable, string PatternMatchingScript, string ContentScript)
Adds a push notification rule to the client account.
async Task RemoveRuleAsync(MessageType MessageType, string LocalName, string Namespace)
Removes a push notification rule from the client account.
async Task ClearRulesAsync()
Clears available push notification rules for the client.
Task NewToken(string Token, PushMessagingService Service, ClientType ClientType, EventHandlerAsync< IqResultEventArgs > Callback, object State)
Reports a new push token to the server.
Manages an XMPP client connection. Implements XMPP, as defined in https://tools.ietf....
Definition: XmppClient.cs:58
Task< uint > SendIqSet(string To, string Xml, EventHandlerAsync< IqResultEventArgs > Callback, object State)
Sends an IQ Set request.
Definition: XmppClient.cs:3646
Base class for XMPP Extensions.
XmppClient client
XMPP Client used by the extension.
void Exception(Exception Exception)
Called to inform the viewer of an exception state.
XmppClient Client
XMPP Client.
ClientType
Type of client requesting notification.
Definition: ClientType.cs:7
PushMessagingService
Push messaging service used.
MessageType
Type of message received.
Definition: MessageType.cs:7