Skip to content

Commit 559c56a

Browse files
committed
Blackberry test to BIS works...
1 parent 1b6207d commit 559c56a

File tree

4 files changed

+201
-70
lines changed

4 files changed

+201
-70
lines changed

PushSharp.Blackberry/BlackberryNotification.cs

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public BlackberryNotification()
2323
{
2424
PushId = Guid.NewGuid ().ToString ();
2525
Recipients = new List<BlackberryRecipient> ();
26-
ContentType = "text/plain";
26+
DeliverBeforeTimestamp = DateTime.UtcNow.AddSeconds(30);
2727
}
2828

2929
public string PushId { get; private set; }
@@ -32,15 +32,15 @@ public BlackberryNotification()
3232
public DateTime? DeliverBeforeTimestamp { get; set; }
3333
public DateTime? DeliverAfterTimestamp { get; set; }
3434
public List<BlackberryRecipient> Recipients { get;set; }
35+
public string SourceReference { get; set; }
3536

36-
public string ContentType { get; set; }
37-
public byte[] Message { get; set; }
37+
public BlackberryMessageContent Content { get; set; }
3838

3939
public string ToPapXml()
4040
{
4141
var doc = new XDocument ();
4242

43-
var docType = new XDocumentType ("pap", "//WAPFORUM//DTD PAP 2.0//EN", "http://www.wapforum.org/DTD/pap_2.0.dtd", "[<?wap-pap-ver supported-versions=\"2.0\"?>]");
43+
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\"?>");
4444

4545
doc.AddFirst (docType);
4646

@@ -49,6 +49,7 @@ public string ToPapXml()
4949
var pushMsg = new XElement ("push-message");
5050

5151
pushMsg.Add (new XAttribute ("push-id", this.PushId));
52+
pushMsg.Add(new XAttribute("source-reference", this.SourceReference));
5253

5354
if (this.QualityOfService.HasValue && !string.IsNullOrEmpty (this.PpgNotifyRequestedTo))
5455
pushMsg.Add(new XAttribute("ppg-notify-requested-to", this.PpgNotifyRequestedTo));
@@ -61,19 +62,27 @@ public string ToPapXml()
6162
//Add all the recipients
6263
foreach (var r in Recipients)
6364
{
64-
var address = new XElement ("address");
65-
var addrValue = string.Format ("WAPPUSH={0}%3A{1}/TYPE={2}", System.Web.HttpUtility.UrlEncode(r.Recipient), r.Port, r.RecipientType);
65+
var address = new XElement("address");
6666

67-
address.Add(new XAttribute("address-value", addrValue));
68-
pushMsg.Add (address);
67+
var addrValue = r.Recipient;
68+
69+
if (!string.IsNullOrEmpty(r.RecipientType))
70+
{
71+
addrValue = string.Format("WAPPUSH={0}%3A{1}/TYPE={2}", System.Web.HttpUtility.UrlEncode(r.Recipient),
72+
r.Port, r.RecipientType);
73+
}
74+
75+
address.Add(new XAttribute("address-value", addrValue));
76+
pushMsg.Add (address);
6977
}
7078

7179
if (this.QualityOfService.HasValue)
7280
pushMsg.Add (new XElement ("quality-of-service", new XAttribute ("delivery-method", this.QualityOfService.Value.ToString ().ToLowerInvariant ())));
7381

82+
pap.Add(pushMsg);
7483
doc.Add (pap);
7584

76-
return doc.ToString (SaveOptions.DisableFormatting);
85+
return "<?xml version=\"1.0\"?>" + Environment.NewLine + doc.ToString (SaveOptions.None);
7786
}
7887

7988

@@ -86,8 +95,50 @@ protected string XmlEncode(string text)
8695

8796
public class BlackberryRecipient
8897
{
98+
public BlackberryRecipient(string recipient)
99+
{
100+
Recipient = recipient;
101+
}
102+
103+
public BlackberryRecipient(string recipient, int port, string recipientType)
104+
{
105+
Recipient = recipient;
106+
Port = port;
107+
RecipientType = recipientType;
108+
}
109+
89110
public string Recipient { get;set; }
90111
public int Port { get;set; }
91112
public string RecipientType { get;set; }
92113
}
114+
115+
public class BlackberryMessageContent
116+
{
117+
118+
public BlackberryMessageContent(string contentType, string content)
119+
{
120+
this.Headers = new Dictionary<string, string>();
121+
this.ContentType = contentType;
122+
this.Content = Encoding.UTF8.GetBytes(content);
123+
}
124+
125+
public BlackberryMessageContent(string content)
126+
{
127+
this.Headers = new Dictionary<string, string>();
128+
this.ContentType = "text/plain";
129+
this.Content = Encoding.UTF8.GetBytes(content);
130+
}
131+
132+
public BlackberryMessageContent(string contentType, byte[] content)
133+
{
134+
this.Headers = new Dictionary<string, string>();
135+
this.ContentType = contentType;
136+
this.Content = content;
137+
}
138+
139+
public string ContentType { get; private set; }
140+
public byte[] Content { get; private set; }
141+
142+
public Dictionary<string, string> Headers { get; private set; }
143+
}
93144
}

PushSharp.Blackberry/BlackberryPushChannel.cs

Lines changed: 94 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Globalization;
33
using System.Linq;
44
using System.Net;
5+
using System.Net.Http.Headers;
56
using System.Text;
67
using System.Xml.Linq;
78
using PushSharp.Core;
@@ -17,90 +18,122 @@ public class BlackberryPushChannel : IPushChannel
1718
public BlackberryPushChannel(BlackberryPushChannelSettings channelSettings)
1819
{
1920
bisChannelSettings = channelSettings;
21+
22+
http = new BlackberryHttpClient(bisChannelSettings);
2023
}
2124

2225
public class BlackberryHttpClient : HttpClient
2326
{
24-
public BlackberryHttpClient() : base()
27+
private BlackberryPushChannelSettings channelSettings;
28+
29+
public BlackberryHttpClient(BlackberryPushChannelSettings channelSettings) : base()
2530
{
31+
this.channelSettings = channelSettings;
32+
33+
var authInfo = channelSettings.ApplicationId + ":" + channelSettings.Password;
34+
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
35+
36+
this.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authInfo);
37+
this.DefaultRequestHeaders.ConnectionClose = true;
38+
39+
this.DefaultRequestHeaders.Remove("connection");
2640
}
2741

2842
public Task<HttpResponseMessage> PostNotification(BlackberryPushChannelSettings channelSettings, BlackberryNotification n)
2943
{
30-
var authInfo = channelSettings.ApplicationId + ":" + channelSettings.Password;
31-
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
32-
3344
var c = new MultipartContent ("related", channelSettings.Boundary);
34-
c.Headers.TryAddWithoutValidation ("Authorization", "Basic " + authInfo);
35-
c.Headers.TryAddWithoutValidation ("Content-Type", "type: application/xml");
45+
c.Headers.Remove("Content-Type");
46+
c.Headers.TryAddWithoutValidation("Content-Type", "multipart/related; boundary=" + channelSettings.Boundary + "; type=application/xml");
47+
3648
var xml = n.ToPapXml ();
3749

38-
c.Add (new StringContent (xml.ToString(), Encoding.UTF8, "application/xml"));
3950

40-
c.Add (new ByteArrayContent(n.Message));
51+
c.Add (new StringContent (xml, Encoding.UTF8, "application/xml"));
52+
53+
var bc = new ByteArrayContent(n.Content.Content);
54+
bc.Headers.Add("Content-Type", n.Content.ContentType);
55+
56+
foreach (var header in n.Content.Headers)
57+
bc.Headers.Add(header.Key, header.Value);
58+
59+
c.Add(bc);
4160

4261
return PostAsync (channelSettings.SendUrl, c);
4362
}
4463
}
4564

46-
BlackberryHttpClient http = new BlackberryHttpClient ();
65+
private BlackberryHttpClient http;
4766

4867
public void SendNotification(INotification notification, SendNotificationCallbackDelegate callback)
4968
{
5069
var n = notification as BlackberryNotification;
5170

52-
var response = http.PostNotification (bisChannelSettings, n).Result;
53-
var description = string.Empty;
54-
55-
var status = new BlackberryMessageStatus
56-
{
57-
Notification = n,
58-
HttpStatus = HttpStatusCode.ServiceUnavailable
59-
};
60-
61-
var bbNotStatus = string.Empty;
62-
status.HttpStatus = response.StatusCode;
63-
64-
var doc = XDocument.Load (response.Content.ReadAsStreamAsync ().Result);
65-
66-
var result = doc.Descendants("response-result").SingleOrDefault();
67-
if (result != null)
68-
{
69-
bbNotStatus = result.Attribute("code").Value;
70-
description = result.Attribute("desc").Value;
71-
}
72-
else
73-
{
74-
result = doc.Descendants("badmessage-response").SingleOrDefault();
75-
if (result != null)
76-
{
77-
bbNotStatus = result.Attribute("code").Value;
78-
description = result.Attribute("desc").Value;
79-
}
80-
}
81-
82-
BlackberryNotificationStatus notStatus;
83-
Enum.TryParse(bbNotStatus, true, out notStatus);
84-
status.NotificationStatus = notStatus;
85-
86-
if (status.NotificationStatus == BlackberryNotificationStatus.NoAppReceivePush)
87-
{
88-
if (callback != null)
89-
callback(this, new SendNotificationResult(notification, false, new Exception("Device Subscription Expired")) { IsSubscriptionExpired = true });
90-
91-
return;
92-
}
93-
94-
if (status.HttpStatus == HttpStatusCode.OK
95-
&& status.NotificationStatus == BlackberryNotificationStatus.RequestAcceptedForProcessing)
96-
{
97-
if (callback != null)
98-
callback(this, new SendNotificationResult(notification));
99-
return;
100-
}
101-
102-
if (callback != null)
103-
callback(this, new SendNotificationResult(status.Notification, false, new BisNotificationSendFailureException(status, description)));
71+
try
72+
{
73+
var response = http.PostNotification(bisChannelSettings, n).Result;
74+
var description = string.Empty;
75+
76+
var status = new BlackberryMessageStatus
77+
{
78+
Notification = n,
79+
HttpStatus = HttpStatusCode.ServiceUnavailable
80+
};
81+
82+
var bbNotStatus = string.Empty;
83+
status.HttpStatus = response.StatusCode;
84+
85+
var doc = XDocument.Load(response.Content.ReadAsStreamAsync().Result);
86+
87+
var result = doc.Descendants("response-result").SingleOrDefault();
88+
if (result != null)
89+
{
90+
bbNotStatus = result.Attribute("code").Value;
91+
description = result.Attribute("desc").Value;
92+
}
93+
else
94+
{
95+
result = doc.Descendants("badmessage-response").SingleOrDefault();
96+
if (result != null)
97+
{
98+
bbNotStatus = result.Attribute("code").Value;
99+
description = result.Attribute("desc").Value;
100+
}
101+
}
102+
103+
BlackberryNotificationStatus notStatus;
104+
Enum.TryParse(bbNotStatus, true, out notStatus);
105+
status.NotificationStatus = notStatus;
106+
107+
if (status.NotificationStatus == BlackberryNotificationStatus.NoAppReceivePush)
108+
{
109+
if (callback != null)
110+
callback(this,
111+
new SendNotificationResult(notification, false, new Exception("Device Subscription Expired"))
112+
{
113+
IsSubscriptionExpired = true
114+
});
115+
116+
return;
117+
}
118+
119+
if (status.HttpStatus == HttpStatusCode.OK
120+
&& status.NotificationStatus == BlackberryNotificationStatus.RequestAcceptedForProcessing)
121+
{
122+
if (callback != null)
123+
callback(this, new SendNotificationResult(notification));
124+
return;
125+
}
126+
127+
if (callback != null)
128+
callback(this,
129+
new SendNotificationResult(status.Notification, false,
130+
new BisNotificationSendFailureException(status, description)));
131+
}
132+
catch (Exception ex)
133+
{
134+
if (callback != null)
135+
callback(this, new SendNotificationResult(notification, false, ex));
136+
}
104137
}
105138

106139
public void Dispose()
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using NUnit.Framework;
3+
using PushSharp.Blackberry;
4+
using System.Threading;
5+
6+
namespace PushSharp.Tests
7+
{
8+
public class BlackberryTests
9+
{
10+
public BlackberryTests ()
11+
{
12+
}
13+
14+
[Test]
15+
public void Blackberry_Simple_Test()
16+
{
17+
var waitCallback = new ManualResetEvent (false);
18+
19+
var settings = new BlackberryPushChannelSettings("APPID", "PWD");
20+
21+
var c = new BlackberryPushChannel (settings);
22+
23+
var n = new BlackberryNotification ();
24+
n.Content = new BlackberryMessageContent("text/plain", "66");
25+
26+
n.QualityOfService = QualityOfServiceLevel.Unconfirmed;
27+
n.SourceReference = "3682-8s4B97Rc2388i46I8M08769D005ra6286o4";
28+
n.DeliverBeforeTimestamp = DateTime.UtcNow.AddMinutes(1);
29+
var r = new BlackberryRecipient ("push_all");
30+
31+
n.Recipients.Add (r);
32+
33+
c.SendNotification (n, (sender, resp) => {
34+
35+
waitCallback.Set();
36+
});
37+
38+
waitCallback.WaitOne ();
39+
}
40+
}
41+
}
42+

Tests/PushSharp.Tests/PushSharp.Tests.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
<Compile Include="PushServiceTests.cs" />
6161
<Compile Include="TestServers\GcmTestServer.cs" />
6262
<Compile Include="TestServers\ApnsTestServer.cs" />
63+
<Compile Include="BlackberryTests.cs" />
6364
</ItemGroup>
6465
<ItemGroup>
6566
<None Include="packages.config" />
@@ -85,6 +86,10 @@
8586
<Project>{0EC3A31E-B869-4465-ABDC-90C2E3CCC17D}</Project>
8687
<Name>PushSharp.Windows</Name>
8788
</ProjectReference>
89+
<ProjectReference Include="..\..\PushSharp.Blackberry\PushSharp.Blackberry.csproj">
90+
<Project>{5250980B-BD11-4201-B083-AEDB8C62C471}</Project>
91+
<Name>PushSharp.Blackberry</Name>
92+
</ProjectReference>
8893
</ItemGroup>
8994
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
9095
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />

0 commit comments

Comments
 (0)