Skip to content

Commit 8f01a2e

Browse files
committed
Push message Topic and Urgency
1 parent a551791 commit 8f01a2e

File tree

12 files changed

+52
-19
lines changed

12 files changed

+52
-19
lines changed

Demo.AspNetCore.PushNotifications.Services.Abstractions/Demo.AspNetCore.PushNotifications.Services.Abstractions.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="Lib.Net.Http.WebPush" Version="1.1.0" />
8+
<PackageReference Include="Lib.Net.Http.WebPush" Version="1.2.0" />
99
</ItemGroup>
1010

1111
</Project>

Demo.AspNetCore.PushNotifications.Services.Abstractions/IPushNotificationService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ public interface IPushNotificationService
66
{
77
string PublicKey { get; }
88

9-
void SendNotification(PushSubscription subscription, string payload);
9+
void SendNotification(PushSubscription subscription, PushMessage message);
1010
}
1111
}

Demo.AspNetCore.PushNotifications.Services.PushService/Demo.AspNetCore.PushNotifications.Services.PushService.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<LangVersion>latest</LangVersion>
55
</PropertyGroup>
66
<ItemGroup>
7-
<PackageReference Include="Lib.Net.Http.WebPush" Version="1.1.0" />
7+
<PackageReference Include="Lib.Net.Http.WebPush" Version="1.2.0" />
88
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.0.0" />
99
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.0.0" />
1010
<PackageReference Include="Microsoft.Extensions.Options" Version="2.0.0" />

Demo.AspNetCore.PushNotifications.Services.PushService/PushServicePushNotificationService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ public PushServicePushNotificationService(IOptions<PushNotificationServiceOption
3232
_logger = logger;
3333
}
3434

35-
public void SendNotification(PushSubscription subscription, string payload)
35+
public void SendNotification(PushSubscription subscription, PushMessage message)
3636
{
3737
try
3838
{
39-
_pushClient.RequestPushMessageDeliveryAsync(subscription, new PushMessage(payload)).Wait();
39+
_pushClient.RequestPushMessageDeliveryAsync(subscription, message).Wait();
4040
}
4141
catch (Exception ex)
4242
{

Demo.AspNetCore.PushNotifications.Services.Sqlite/Demo.AspNetCore.PushNotifications.Services.Sqlite.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="Lib.Net.Http.WebPush" Version="1.1.0" />
8+
<PackageReference Include="Lib.Net.Http.WebPush" Version="1.2.0" />
99
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.0.1" />
1010
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.0.1" />
1111
</ItemGroup>

Demo.AspNetCore.PushNotifications/Controllers/PushNotificationsApiController.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Threading.Tasks;
22
using Microsoft.AspNetCore.Mvc;
33
using Lib.Net.Http.WebPush;
4+
using Demo.AspNetCore.PushNotifications.Model;
45
using Demo.AspNetCore.PushNotifications.Services.Abstractions;
56

67
namespace Demo.AspNetCore.PushNotifications.Controllers
@@ -44,10 +45,16 @@ public async Task<IActionResult> DiscardSubscription(string endpoint)
4445

4546
// POST push-notifications-api/notifications
4647
[HttpPost("notifications")]
47-
public async Task<IActionResult> SendNotification([FromBody]string payload)
48+
public async Task<IActionResult> SendNotification([FromBody]PushMessageViewModel message)
4849
{
50+
PushMessage pushMessage = new PushMessage(message.Notification)
51+
{
52+
Topic = message.Topic,
53+
Urgency = message.Urgency
54+
};
55+
4956
// TODO: This should be scheduled in background
50-
await _subscriptionStore.ForEachSubscriptionAsync((PushSubscription subscription) => _notificationService.SendNotification(subscription, payload));
57+
await _subscriptionStore.ForEachSubscriptionAsync((PushSubscription subscription) => _notificationService.SendNotification(subscription, pushMessage));
5158

5259
return NoContent();
5360
}

Demo.AspNetCore.PushNotifications/Demo.AspNetCore.PushNotifications.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<TargetFramework>netcoreapp2.0</TargetFramework>
44
</PropertyGroup>
55
<ItemGroup>
6-
<PackageReference Include="Lib.Net.Http.WebPush" Version="1.1.0" />
6+
<PackageReference Include="Lib.Net.Http.WebPush" Version="1.2.0" />
77
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
88
</ItemGroup>
99
<ItemGroup>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Lib.Net.Http.WebPush;
2+
3+
namespace Demo.AspNetCore.PushNotifications.Model
4+
{
5+
public class PushMessageViewModel
6+
{
7+
public string Topic { get; set; }
8+
9+
public string Notification { get; set; }
10+
11+
public PushMessageUrgency Urgency { get; set; } = PushMessageUrgency.Normal;
12+
}
13+
}

Demo.AspNetCore.PushNotifications/Startup.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.AspNetCore.Http;
44
using Microsoft.Extensions.Configuration;
55
using Microsoft.Extensions.DependencyInjection;
6+
using Newtonsoft.Json.Converters;
67
using Demo.AspNetCore.PushNotifications.Services;
78
using Demo.AspNetCore.PushNotifications.Formatters;
89

@@ -24,6 +25,10 @@ public void ConfigureServices(IServiceCollection services)
2425
.AddMvc(options =>
2526
{
2627
options.InputFormatters.Add(new TextPlainInputFormatter());
28+
})
29+
.AddJsonOptions(options =>
30+
{
31+
options.SerializerSettings.Converters.Add(new StringEnumConverter());
2732
});
2833
}
2934

Demo.AspNetCore.PushNotifications/appsettings.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
"PushNotificationServiceType": "PushService",
66
"PushNotificationService": {
77
"Subject": "https://localhost:65506/",
8-
"PublicKey": "BK5sn4jfa0Jqo9MhV01oyzK2FaEHm0KqkSCuUkKr53-9cr-vBE1a9TiiBaWy7hy0eOUF1jhZnwcd3vof4wnwSw0",
9-
//"PublicKey": "<Application Server Public Key>",
10-
"PrivateKey": "AJ2ho7or-6D4StPktpTO3l1ErjHGyxb0jzt9Y8lj67g"
11-
//"PrivateKey": "<Application Server Private Key>"
8+
"PublicKey": "<Application Server Public Key>",
9+
"PrivateKey": "<Application Server Private Key>"
1210
}
1311
}

0 commit comments

Comments
 (0)