Skip to content

Commit 42b5367

Browse files
author
Cory Thompson
committedApr 7, 2017
#8 - Add SendPushNotificationAsync
1 parent 757f462 commit 42b5367

File tree

3 files changed

+106
-30
lines changed

3 files changed

+106
-30
lines changed
 

‎src/WebPush.csproj

+36-1
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,47 @@
3535
<HintPath>..\packages\BouncyCastle.1.8.1\lib\BouncyCastle.Crypto.dll</HintPath>
3636
<Private>True</Private>
3737
</Reference>
38+
<Reference Include="Microsoft.Threading.Tasks, Version=1.0.12.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
39+
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.168\lib\net40\Microsoft.Threading.Tasks.dll</HintPath>
40+
</Reference>
41+
<Reference Include="Microsoft.Threading.Tasks.Extensions, Version=1.0.12.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
42+
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.168\lib\net40\Microsoft.Threading.Tasks.Extensions.dll</HintPath>
43+
</Reference>
44+
<Reference Include="Microsoft.Threading.Tasks.Extensions.Desktop, Version=1.0.168.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
45+
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.168\lib\net40\Microsoft.Threading.Tasks.Extensions.Desktop.dll</HintPath>
46+
</Reference>
3847
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
3948
<HintPath>..\packages\Newtonsoft.Json.6.0.1\lib\net40\Newtonsoft.Json.dll</HintPath>
4049
<Private>True</Private>
4150
</Reference>
4251
<Reference Include="System" />
4352
<Reference Include="System.Core" />
53+
<Reference Include="System.IO, Version=2.6.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
54+
<HintPath>..\packages\Microsoft.Bcl.1.1.10\lib\net40\System.IO.dll</HintPath>
55+
</Reference>
56+
<Reference Include="System.Net" />
57+
<Reference Include="System.Net.Http, Version=2.2.29.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
58+
<HintPath>..\packages\Microsoft.Net.Http.2.2.29\lib\net40\System.Net.Http.dll</HintPath>
59+
</Reference>
60+
<Reference Include="System.Net.Http.Extensions, Version=2.2.29.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
61+
<HintPath>..\packages\Microsoft.Net.Http.2.2.29\lib\net40\System.Net.Http.Extensions.dll</HintPath>
62+
</Reference>
63+
<Reference Include="System.Net.Http.Primitives, Version=2.2.29.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
64+
<HintPath>..\packages\Microsoft.Net.Http.2.2.29\lib\net40\System.Net.Http.Primitives.dll</HintPath>
65+
</Reference>
66+
<Reference Include="System.Net.Http.WebRequest, Version=2.2.29.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
67+
<HintPath>..\packages\Microsoft.Net.Http.2.2.29\lib\net40\System.Net.Http.WebRequest.dll</HintPath>
68+
</Reference>
69+
<Reference Include="System.Runtime, Version=2.6.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
70+
<HintPath>..\packages\Microsoft.Bcl.1.1.10\lib\net40\System.Runtime.dll</HintPath>
71+
</Reference>
72+
<Reference Include="System.Threading.Tasks, Version=2.6.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
73+
<HintPath>..\packages\Microsoft.Bcl.1.1.10\lib\net40\System.Threading.Tasks.dll</HintPath>
74+
</Reference>
4475
<Reference Include="System.Xml.Linq" />
4576
<Reference Include="System.Data.DataSetExtensions" />
4677
<Reference Include="Microsoft.CSharp" />
4778
<Reference Include="System.Data" />
48-
<Reference Include="System.Net.Http" />
4979
<Reference Include="System.Xml" />
5080
</ItemGroup>
5181
<ItemGroup>
@@ -66,6 +96,11 @@
6696
<None Include="packages.config" />
6797
</ItemGroup>
6898
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
99+
<Import Project="..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />
100+
<Target Name="EnsureBclBuildImported" BeforeTargets="BeforeBuild" Condition="'$(BclBuildImported)' == ''">
101+
<Error Condition="!Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=317567." HelpKeyword="BCLBUILD2001" />
102+
<Error Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568." HelpKeyword="BCLBUILD2002" />
103+
</Target>
69104
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
70105
Other similar extension points exist, see Microsoft.Common.targets.
71106
<Target Name="BeforeBuild">

‎src/WebPushClient.cs

+66-29
Original file line numberDiff line numberDiff line change
@@ -231,52 +231,89 @@ public HttpRequestMessage GenerateRequestDetails(PushSubscription subscription,
231231
return request;
232232
}
233233

234-
/// <summary>
235-
/// To send a push notification call this method with a subscription, optional payload and any options
236-
/// Will exception is unsuccessful
237-
/// </summary>
238-
/// <param name="subscription">The PushSubscription you wish to send the notification to.</param>
239-
/// <param name="payload">The payload you wish to send to the user</param>
240-
/// <param name="options">Options for the GCM API key and vapid keys can be passed in if they are unique for each notification.</param>
241-
public void SendNotification(PushSubscription subscription, string payload = null, Dictionary<string, object> options = null)
234+
/// <summary>
235+
/// To send a push notification call this method with a subscription, optional payload and any options
236+
/// Will exception is unsuccessful
237+
/// </summary>
238+
/// <param name="subscription">The PushSubscription you wish to send the notification to.</param>
239+
/// <param name="payload">The payload you wish to send to the user</param>
240+
/// <param name="options">Options for the GCM API key and vapid keys can be passed in if they are unique for each notification.</param>
241+
public void SendNotification(PushSubscription subscription, string payload = null, Dictionary<string, object> options = null)
242+
{
243+
SendNotificationAsync(subscription, payload, options).Wait();
244+
}
245+
246+
/// <summary>
247+
/// To send a push notification call this method with a subscription, optional payload and any options
248+
/// Will exception is unsuccessful
249+
/// </summary>
250+
/// <param name="subscription">The PushSubscription you wish to send the notification to.</param>
251+
/// <param name="payload">The payload you wish to send to the user</param>
252+
/// <param name="vapidDetails">The vapid details for the notification.</param>
253+
public void SendNotification(PushSubscription subscription, string payload, VapidDetails vapidDetails)
254+
{
255+
SendNotificationAsync(subscription, payload, vapidDetails).Wait();
256+
}
257+
258+
/// <summary>
259+
/// To send a push notification call this method with a subscription, optional payload and any options
260+
/// Will exception is unsuccessful
261+
/// </summary>
262+
/// <param name="subscription">The PushSubscription you wish to send the notification to.</param>
263+
/// <param name="payload">The payload you wish to send to the user</param>
264+
/// <param name="gcmAPIKey">The GCM API key</param>
265+
public void SendNotification(PushSubscription subscription, string payload, string gcmAPIKey)
266+
{
267+
SendNotificationAsync(subscription, payload, gcmAPIKey).Wait();
268+
}
269+
270+
/// <summary>
271+
/// To send a push notification asyncronously call this method with a subscription, optional payload and any options
272+
/// Will exception is unsuccessful
273+
/// </summary>
274+
/// <param name="subscription">The PushSubscription you wish to send the notification to.</param>
275+
/// <param name="payload">The payload you wish to send to the user</param>
276+
/// <param name="options">Options for the GCM API key and vapid keys can be passed in if they are unique for each notification.</param>
277+
public async Task SendNotificationAsync(PushSubscription subscription, string payload = null, Dictionary<string, object> options = null)
242278
{
279+
243280
HttpRequestMessage request = GenerateRequestDetails(subscription, payload, options);
244-
Task<HttpResponseMessage> webTask = httpClient.SendAsync(request);
245-
webTask.Wait();
246281

247-
HttpResponseMessage response = webTask.Result;
282+
HttpResponseMessage response = await httpClient.SendAsync(request);
283+
248284
if (response.StatusCode != System.Net.HttpStatusCode.Created) //201
249285
{
250286
throw new WebPushException(@"Received unexpected response code", response.StatusCode, response.Headers, subscription);
251287
}
288+
252289
}
253290

254-
/// <summary>
255-
/// To send a push notification call this method with a subscription, optional payload and any options
256-
/// Will exception is unsuccessful
257-
/// </summary>
258-
/// <param name="subscription">The PushSubscription you wish to send the notification to.</param>
259-
/// <param name="payload">The payload you wish to send to the user</param>
260-
/// <param name="vapidDetails">The vapid details for the notification.</param>
261-
public void SendNotification(PushSubscription subscription, string payload, VapidDetails vapidDetails)
291+
/// <summary>
292+
/// To send a push notification asyncronously call this method with a subscription, optional payload and any options
293+
/// Will exception is unsuccessful
294+
/// </summary>
295+
/// <param name="subscription">The PushSubscription you wish to send the notification to.</param>
296+
/// <param name="payload">The payload you wish to send to the user</param>
297+
/// <param name="vapidDetails">The vapid details for the notification.</param>
298+
public async Task SendNotificationAsync(PushSubscription subscription, string payload, VapidDetails vapidDetails)
262299
{
263300
Dictionary<string, object> options = new Dictionary<string, object>();
264301
options["vapidDetails"] = vapidDetails;
265-
SendNotification(subscription, payload, options);
302+
await SendNotificationAsync(subscription, payload, options);
266303
}
267304

268-
/// <summary>
269-
/// To send a push notification call this method with a subscription, optional payload and any options
270-
/// Will exception is unsuccessful
271-
/// </summary>
272-
/// <param name="subscription">The PushSubscription you wish to send the notification to.</param>
273-
/// <param name="payload">The payload you wish to send to the user</param>
274-
/// <param name="gcmAPIKey">The GCM API key</param>
275-
public void SendNotification(PushSubscription subscription, string payload, string gcmAPIKey)
305+
/// <summary>
306+
/// To send a push notification asyncronously call this method with a subscription, optional payload and any options
307+
/// Will exception is unsuccessful
308+
/// </summary>
309+
/// <param name="subscription">The PushSubscription you wish to send the notification to.</param>
310+
/// <param name="payload">The payload you wish to send to the user</param>
311+
/// <param name="gcmAPIKey">The GCM API key</param>
312+
public async Task SendNotificationAsync(PushSubscription subscription, string payload, string gcmAPIKey)
276313
{
277314
Dictionary<string, object> options = new Dictionary<string, object>();
278315
options["gcmAPIKey"] = gcmAPIKey;
279-
SendNotification(subscription, payload, options);
316+
await SendNotificationAsync(subscription, payload, options);
280317
}
281318
}
282319
}

‎src/packages.config

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="BouncyCastle" version="1.8.1" targetFramework="net40" />
4+
<package id="Microsoft.Bcl" version="1.1.10" targetFramework="net40" />
5+
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net40" />
6+
<package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net40" />
7+
<package id="Microsoft.Net.Http" version="2.2.29" targetFramework="net40" />
48
<package id="Newtonsoft.Json" version="6.0.1" targetFramework="net40" />
59
</packages>

0 commit comments

Comments
 (0)
Please sign in to comment.