forked from Redth/PushSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlackberryNotification.cs
168 lines (128 loc) · 5.48 KB
/
BlackberryNotification.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System;
using PushSharp.Core;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Text;
using System.Globalization;
namespace PushSharp.Blackberry
{
public enum QualityOfServiceLevel
{
NotSpecified,
Unconfirmed,
PreferConfirmed,
Confirmed
}
public class BlackberryNotification : INotification
{
public BlackberryNotification()
{
PushId = Guid.NewGuid ().ToString ();
Recipients = new List<BlackberryRecipient> ();
DeliverBeforeTimestamp = DateTime.UtcNow.AddMinutes(5);
QualityOfService = QualityOfServiceLevel.Unconfirmed;
}
public bool IsDeviceRegistrationIdValid ()
{
return true;
}
public object Tag { get;set; }
public string PushId { get; private set; }
public QualityOfServiceLevel QualityOfService { get;set; }
/// <summary>
/// Address (e.g. URL) that Blackberry push service could use for notification
/// of results related to the message
/// </summary>
public string PpgNotifyRequestedTo { get; set; }
/// <summary>
/// Date and time by which the content must be delivered,expressed as UTC
/// Message that has aged beyond this date will not be transmitted
/// </summary>
public DateTime? DeliverBeforeTimestamp { get; set; }
/// <summary>
/// Date and time after which the content should be delivered,expressed as UTC
/// Message will not be transmitted before this date
/// </summary>
public DateTime? DeliverAfterTimestamp { get; set; }
public List<BlackberryRecipient> Recipients { get;set; }
public string SourceReference { get; set; }
public BlackberryMessageContent Content { get; set; }
public string ToPapXml()
{
var doc = new XDocument ();
var docType = new XDocumentType("pap", "-//WAPFORUM//DTD PAP 2.1//EN", "http://www.openmobilealliance.org/tech/DTD/pap_2.1.dtd", "<?wap-pap-ver supported-versions=\"2.0\"?>");
doc.AddFirst (docType);
var pap = new XElement ("pap");
var pushMsg = new XElement ("push-message");
pushMsg.Add (new XAttribute ("push-id", this.PushId));
pushMsg.Add(new XAttribute("source-reference", this.SourceReference));
if (!string.IsNullOrEmpty (this.PpgNotifyRequestedTo))
pushMsg.Add(new XAttribute("ppg-notify-requested-to", this.PpgNotifyRequestedTo));
if (this.DeliverAfterTimestamp.HasValue)
pushMsg.Add (new XAttribute ("deliver-after-timestamp", this.DeliverAfterTimestamp.Value.ToUniversalTime ().ToString("s", CultureInfo.InvariantCulture) + "Z"));
if (this.DeliverBeforeTimestamp.HasValue)
pushMsg.Add (new XAttribute ("deliver-before-timestamp", this.DeliverBeforeTimestamp.Value.ToUniversalTime ().ToString("s", CultureInfo.InvariantCulture) + "Z"));
//Add all the recipients
foreach (var r in Recipients)
{
var address = new XElement("address");
var addrValue = r.Recipient;
if (!string.IsNullOrEmpty(r.RecipientType))
{
addrValue = string.Format("WAPPUSH={0}%3A{1}/TYPE={2}", System.Web.HttpUtility.UrlEncode(r.Recipient),
r.Port, r.RecipientType);
}
address.Add(new XAttribute("address-value", addrValue));
pushMsg.Add (address);
}
pushMsg.Add (new XElement ("quality-of-service", new XAttribute ("delivery-method", this.QualityOfService.ToString ().ToLowerInvariant ())));
pap.Add(pushMsg);
doc.Add (pap);
return "<?xml version=\"1.0\"?>" + Environment.NewLine + doc.ToString (SaveOptions.None);
}
protected string XmlEncode(string text)
{
return System.Security.SecurityElement.Escape(text);
}
}
public class BlackberryRecipient
{
public BlackberryRecipient(string recipient)
{
Recipient = recipient;
}
public BlackberryRecipient(string recipient, int port, string recipientType)
{
Recipient = recipient;
Port = port;
RecipientType = recipientType;
}
public string Recipient { get;set; }
public int Port { get;set; }
public string RecipientType { get;set; }
}
public class BlackberryMessageContent
{
public BlackberryMessageContent(string contentType, string content)
{
this.Headers = new Dictionary<string, string>();
this.ContentType = contentType;
this.Content = Encoding.UTF8.GetBytes(content);
}
public BlackberryMessageContent(string content)
{
this.Headers = new Dictionary<string, string>();
this.ContentType = "text/plain";
this.Content = Encoding.UTF8.GetBytes(content);
}
public BlackberryMessageContent(string contentType, byte[] content)
{
this.Headers = new Dictionary<string, string>();
this.ContentType = contentType;
this.Content = content;
}
public string ContentType { get; private set; }
public byte[] Content { get; private set; }
public Dictionary<string, string> Headers { get; private set; }
}
}