From cb858ee0bf1b7003ebaa93262dcf6681664d4571 Mon Sep 17 00:00:00 2001 From: Maxwell Weru Date: Mon, 2 Oct 2023 08:25:50 +0300 Subject: [PATCH] Better handle required values in APNs models --- .../Apple/Models/ApnsMessageData.cs | 12 +++--------- .../Apple/Models/ApnsMessageHeader.cs | 2 +- .../Apple/Models/ApnsMessagePayload.cs | 2 +- .../ApnsNotifierTests.cs | 12 +++--------- 4 files changed, 8 insertions(+), 20 deletions(-) diff --git a/src/Tingle.Extensions.PushNotifications/Apple/Models/ApnsMessageData.cs b/src/Tingle.Extensions.PushNotifications/Apple/Models/ApnsMessageData.cs index 4b7d316..4f0e373 100644 --- a/src/Tingle.Extensions.PushNotifications/Apple/Models/ApnsMessageData.cs +++ b/src/Tingle.Extensions.PushNotifications/Apple/Models/ApnsMessageData.cs @@ -4,13 +4,7 @@ namespace Tingle.Extensions.PushNotifications.Apple.Models; /// /// Represents the data actually sent to the device. -/// If you need to send more information, inherit from this class. +/// If you need to send more information, inherit from this type. /// -public class ApnsMessageData -{ - /// - /// The payload for the push as specified by Apple - /// - [JsonPropertyName("aps")] - public ApnsMessagePayload Aps { get; set; } = new ApnsMessagePayload { }; -} +/// The payload for the push as specified by Apple +public record ApnsMessageData([property: JsonPropertyName("aps")] ApnsMessagePayload Aps); diff --git a/src/Tingle.Extensions.PushNotifications/Apple/Models/ApnsMessageHeader.cs b/src/Tingle.Extensions.PushNotifications/Apple/Models/ApnsMessageHeader.cs index d360346..0148d42 100644 --- a/src/Tingle.Extensions.PushNotifications/Apple/Models/ApnsMessageHeader.cs +++ b/src/Tingle.Extensions.PushNotifications/Apple/Models/ApnsMessageHeader.cs @@ -53,5 +53,5 @@ public sealed class ApnsMessageHeader /// /// The token for the device to send the message to /// - public string? DeviceToken { get; set; } + public required string DeviceToken { get; set; } } diff --git a/src/Tingle.Extensions.PushNotifications/Apple/Models/ApnsMessagePayload.cs b/src/Tingle.Extensions.PushNotifications/Apple/Models/ApnsMessagePayload.cs index 47a2c7b..a6b31c8 100644 --- a/src/Tingle.Extensions.PushNotifications/Apple/Models/ApnsMessagePayload.cs +++ b/src/Tingle.Extensions.PushNotifications/Apple/Models/ApnsMessagePayload.cs @@ -5,7 +5,7 @@ namespace Tingle.Extensions.PushNotifications.Apple.Models; /// /// Represents a payload for a push notification as specified by Apple /// -public class ApnsMessagePayload +public record ApnsMessagePayload { /// /// Set a value when you want the system to display a standard alert or a banner. diff --git a/tests/Tingle.Extensions.PushNotifications.Tests/ApnsNotifierTests.cs b/tests/Tingle.Extensions.PushNotifications.Tests/ApnsNotifierTests.cs index 3979121..4c301c0 100644 --- a/tests/Tingle.Extensions.PushNotifications.Tests/ApnsNotifierTests.cs +++ b/tests/Tingle.Extensions.PushNotifications.Tests/ApnsNotifierTests.cs @@ -67,7 +67,7 @@ public async Task Authentication_IsPopulated() cache.Set("apns:tokens:cake:cake", "cake-token"); var client = sp.GetRequiredService(); - var rr = await client.SendAsync(new ApnsMessageHeader { DeviceToken = "cake" }, new ApnsMessageData { }); + var rr = await client.SendAsync(new ApnsMessageHeader { DeviceToken = "cake" }, new ApnsMessageData(new ApnsMessagePayload { })); Assert.Equal("bearer cake-token", header); } @@ -112,17 +112,11 @@ public async Task Works() var header = new ApnsMessageHeader { - DeviceToken = configuration.GetValue("ApnsTest:DeviceToken"), + DeviceToken = configuration.GetValue("ApnsTest:DeviceToken")!, Environment = configuration.GetValue("ApnsTest:Environment") ?? ApnsEnvironment.Development, PushType = ApnsPushType.Background, }; - var data = new ApnsMessageData - { - Aps = new ApnsMessagePayload - { - ContentAvailable = 1 - }, - }; + var data = new ApnsMessageData(new ApnsMessagePayload { ContentAvailable = 1, }); var resp = await client.SendAsync(header, data, default); resp.EnsureSuccess();