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 void Dispose()
33 {
34 base.Dispose();
35 }
36
40 public override string[] Extensions => new string[0];
41
42 #region New Token
43
52 public Task NewToken(string Token, PushMessagingService Service, ClientType ClientType, EventHandlerAsync<IqResultEventArgs> Callback, object State)
53 {
54 StringBuilder Xml = new StringBuilder();
55
56 Xml.Append("<newToken xmlns='");
57 Xml.Append(MessagePushNamespace);
58 Xml.Append("' service='");
59 Xml.Append(Service.ToString());
60 Xml.Append("' clientType='");
61 Xml.Append(ClientType.ToString());
62 Xml.Append("' token='");
63 Xml.Append(XML.Encode(Token));
64 Xml.Append("'/>");
65
66 return this.client.SendIqSet(string.Empty, Xml.ToString(), Callback, State);
67 }
68
75 public async Task NewTokenAsync(string Token, PushMessagingService Service, ClientType ClientType)
76 {
77 TaskCompletionSource<bool> Result = new TaskCompletionSource<bool>();
78
79 await this.NewToken(Token, Service, ClientType, (Sender, e) =>
80 {
81 if (e.Ok)
82 Result.TrySetResult(true);
83 else
84 Result.TrySetException(e.StanzaError ?? new Exception("Unable to report new token."));
85
86 return Task.CompletedTask;
87 }, null);
88
89 await Result.Task;
90 }
91
92 #endregion
93
94 #region Remove Token
95
101 public Task RemoveToken(EventHandlerAsync<IqResultEventArgs> Callback, object State)
102 {
103 StringBuilder Xml = new StringBuilder();
104
105 Xml.Append("<removeToken xmlns='");
106 Xml.Append(MessagePushNamespace);
107 Xml.Append("'/>");
108
109 return this.client.SendIqSet(string.Empty, Xml.ToString(), Callback, State);
110 }
111
115 public async Task RemoveTokenAsync()
116 {
117 TaskCompletionSource<bool> Result = new TaskCompletionSource<bool>();
118
119 await this.RemoveToken((Sender, e) =>
120 {
121 if (e.Ok)
122 Result.TrySetResult(true);
123 else
124 Result.TrySetException(e.StanzaError ?? new Exception("Unable to remove token."));
125
126 return Task.CompletedTask;
127 }, null);
128
129 await Result.Task;
130 }
131
132 #endregion
133
134 #region Clear Rules
135
141 public Task ClearRules(EventHandlerAsync<IqResultEventArgs> Callback, object State)
142 {
143 StringBuilder Xml = new StringBuilder();
144
145 Xml.Append("<clearRules xmlns='");
146 Xml.Append(MessagePushNamespace);
147 Xml.Append("'/>");
148
149 return this.client.SendIqSet(string.Empty, Xml.ToString(), Callback, State);
150 }
151
155 public async Task ClearRulesAsync()
156 {
157 TaskCompletionSource<bool> Result = new TaskCompletionSource<bool>();
158
159 await this.ClearRules((Sender, e) =>
160 {
161 if (e.Ok)
162 Result.TrySetResult(true);
163 else
164 Result.TrySetException(e.StanzaError ?? new Exception("Unable to clear available rules."));
165
166 return Task.CompletedTask;
167 }, null);
168
169 await Result.Task;
170 }
171
172 #endregion
173
174 #region Add Push Notification Rule
175
197 public Task AddRule(MessageType MessageType, string LocalName, string Namespace, string Channel, string MessageVariable,
198 string PatternMatchingScript, string ContentScript, EventHandlerAsync<IqResultEventArgs> Callback, object State)
199 {
200 StringBuilder Xml = new StringBuilder();
201
202 Xml.Append("<addRule xmlns='");
203 Xml.Append(MessagePushNamespace);
204 Xml.Append("' type='");
205
206 if (MessageType != MessageType.Normal)
207 Xml.Append(MessageType.ToString().ToLower());
208
209 Xml.Append("' localName='");
210 Xml.Append(XML.Encode(LocalName));
211 Xml.Append("' namespace='");
212 Xml.Append(XML.Encode(Namespace));
213 Xml.Append("' channel='");
214 Xml.Append(XML.Encode(Channel));
215 Xml.Append("' variable='");
216 Xml.Append(XML.Encode(MessageVariable));
217 Xml.Append("'>");
218
219 if (!string.IsNullOrEmpty(PatternMatchingScript))
220 {
221 Xml.Append("<PatternMatching>");
222 Xml.Append(XML.Encode(PatternMatchingScript));
223 Xml.Append("</PatternMatching>");
224 }
225
226 if (!string.IsNullOrEmpty(ContentScript))
227 {
228 Xml.Append("<Content>");
229 Xml.Append(XML.Encode(ContentScript));
230 Xml.Append("</Content>");
231 }
232
233 Xml.Append("</addRule>");
234
235 return this.client.SendIqSet(string.Empty, Xml.ToString(), Callback, State);
236 }
237
257 public async Task AddRuleAsync(MessageType MessageType, string LocalName, string Namespace, string Channel, string MessageVariable,
258 string PatternMatchingScript, string ContentScript)
259 {
260 TaskCompletionSource<bool> Result = new TaskCompletionSource<bool>();
261
262 await this.AddRule(MessageType, LocalName, Namespace, Channel, MessageVariable, PatternMatchingScript, ContentScript, (Sender, e) =>
263 {
264 if (e.Ok)
265 Result.TrySetResult(true);
266 else
267 Result.TrySetException(e.StanzaError ?? new Exception("Unable to add push notification rule."));
268
269 return Task.CompletedTask;
270 }, null);
271
272 await Result.Task;
273 }
274
275 #endregion
276
277 #region Remove Push Notification Rule
278
287 public Task RemoveRule(MessageType MessageType, string LocalName, string Namespace, EventHandlerAsync<IqResultEventArgs> Callback, object State)
288 {
289 StringBuilder Xml = new StringBuilder();
290
291 Xml.Append("<removeRule xmlns='");
292 Xml.Append(MessagePushNamespace);
293 Xml.Append("' type='");
294
295 if (MessageType != MessageType.Normal)
296 Xml.Append(MessageType.ToString().ToLower());
297
298 Xml.Append("' localName='");
299 Xml.Append(XML.Encode(LocalName));
300 Xml.Append("' namespace='");
301 Xml.Append(XML.Encode(Namespace));
302 Xml.Append("'/>");
303
304 return this.client.SendIqSet(string.Empty, Xml.ToString(), Callback, State);
305 }
306
313 public async Task RemoveRuleAsync(MessageType MessageType, string LocalName, string Namespace)
314 {
315 TaskCompletionSource<bool> Result = new TaskCompletionSource<bool>();
316
317 await this.RemoveRule(MessageType, LocalName, Namespace, (Sender, e) =>
318 {
319 if (e.Ok)
320 Result.TrySetResult(true);
321 else
322 Result.TrySetException(e.StanzaError ?? new Exception("Unable to remove push notification rule."));
323
324 return Task.CompletedTask;
325 }, null);
326
327 await Result.Task;
328 }
329
330 #endregion
331
332 }
333}
Helps with common XML-related tasks.
Definition: XML.cs:19
static string Encode(string s)
Encodes a string for use in XML.
Definition: XML.cs:27
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.
override void Dispose()
Disposes of the extension.
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:59
Task< uint > SendIqSet(string To, string Xml, EventHandlerAsync< IqResultEventArgs > Callback, object State)
Sends an IQ Set request.
Definition: XmppClient.cs:3607
Base class for XMPP Extensions.
XmppClient client
XMPP Client used by the extension.
Task 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