Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Push notifications all override each other #1042

Merged
merged 5 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

namespace Backbone.Modules.Devices.Application.Infrastructure.PushNotifications.Datawallet;

[NotificationId("DatawalletModified")]
public record DatawalletModificationsCreatedPushNotification(string CreatedByDevice) : IPushNotification;
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

namespace Backbone.Modules.Devices.Application.Infrastructure.PushNotifications.ExternalEvents;

[NotificationId("ExternalEventCreated")]
public record ExternalEventCreatedPushNotification : IPushNotification;
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ namespace Backbone.Modules.Devices.Application.Infrastructure.PushNotifications;

public class NotificationIdAttribute : Attribute
{
public NotificationIdAttribute(int value)
public NotificationIdAttribute(string value)
{
#if DEBUG
if (value.Length > 64)
throw new Exception("The value of the NotificationIdAttribute must be less than or equal to 64 characters.");
#endif

Value = value;
}

public int Value { get; }
public string Value { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,14 @@ public ApnsMessageBuilder SetNotificationText(string title, string body)
return this;
}

public ApnsMessageBuilder SetNotificationId(int notificationId)
public ApnsMessageBuilder SetNotificationId(string? notificationId)
{
if (notificationId == null)
return this;

_notification.NotificationId = notificationId;
_request.Headers.Add("apns-collapse-id", notificationId.ToString());
_request.Headers.Add("apns-collapse-id", notificationId);

return this;
}

Expand All @@ -71,7 +75,8 @@ public HttpRequestMessage Build()
private class Payload
{
[JsonPropertyName("notId")]
public int NotificationId { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? NotificationId { get; set; }

[JsonPropertyName("content")]
public NotificationContent? Content { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ private static string BuildUrl(PushEnvironment environment, string handle)
return $"{baseUrl}/{handle}";
}

private static int GetNotificationId(object pushNotification)
private static string? GetNotificationId(object pushNotification)
{
var attribute = pushNotification.GetType().GetCustomAttribute<NotificationIdAttribute>();
return attribute?.Value ?? 0;
return attribute?.Value;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ private void SetContentAvailable(bool contentAvailable)
_data["content-available"] = contentAvailable ? "1" : "0";
}

public FcmMessageBuilder SetTag(int notificationId)
public FcmMessageBuilder SetTag(string? notificationId)
{
_message.Android.Notification.Tag = notificationId.ToString();
_message.Android.CollapseKey = notificationId.ToString();
if (notificationId == null)
return this;

_message.Android.Notification.Tag = notificationId;
_message.Android.CollapseKey = notificationId;

return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ public void ValidateRegistration(PnsRegistration registration)
throw new InfrastructureException(InfrastructureErrors.InvalidPushNotificationConfiguration(_options.GetSupportedAppIds()));
}

private static int GetNotificationId(object pushNotification)
private static string? GetNotificationId(object pushNotification)
{
var attribute = pushNotification.GetType().GetCustomAttribute<NotificationIdAttribute>();
return attribute?.Value ?? 0;
return attribute?.Value ?? null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ApnsMessageBuilderTests : AbstractTestsBase
public void Built_message_has_all_properties_set()
{
// Act
var request = new ApnsMessageBuilder("someAppBundleIdentifier", "https://api.development.push.apple.com/3/device/someDeviceId", "someValidJwt").Build();
var request = new ApnsMessageBuilder("someAppBundleIdentifier", "https://api.development.push.apple.com/3/device/someDeviceId", "someValidJwt").SetNotificationId("testNotificationId").Build();

// Assert
request.RequestUri!.ToString().Should().Contain("https://api.development.push.apple.com/3/device/someDeviceId");
Expand All @@ -23,6 +23,7 @@ public void Built_message_has_all_properties_set()
request.Headers.GetValues("apns-push-type").FirstOrDefault().Should().Be("alert");
request.Headers.GetValues("apns-priority").FirstOrDefault().Should().Be("5");
request.Headers.GetValues("Authorization").FirstOrDefault().Should().NotBeNull();
request.Headers.GetValues("apns-collapse-id").FirstOrDefault().Should().Be("testNotificationId");
}

[Fact]
Expand All @@ -36,15 +37,15 @@ public async Task Content_is_valid_json()
.AddContent(new NotificationContent(IdentityAddress.Parse("did:e:prod.enmeshed.eu:dids:1a7063b5d2c7a8945bf43d"), DevicePushIdentifier.Parse("DPIaaaaaaaaaaaaaaaaa"),
new TestPushNotification { SomeProperty = "someValue" }))
.SetNotificationText("someNotificationTextTitle", "someNotificationTextBody")
.SetNotificationId(1)
.SetNotificationId("testNotificationId")
.Build();
var actualContent = await request.Content!.ReadAsStringAsync();

// Assert
actualContent.Should().BeEquivalentToJson(
"""
{
'notId': 1,
'notId': 'testNotificationId',
'content': {
'accRef': 'did:e:prod.enmeshed.eu:dids:1a7063b5d2c7a8945bf43d',
'devicePushIdentifier' : 'DPIaaaaaaaaaaaaaaaaa',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void Built_message_has_all_properties_set()
{
// Act
var message = new FcmMessageBuilder()
.SetTag(1)
.SetTag("testNotificationId")
.SetToken("token1")
.SetNotificationText("someNotificationTextTitle", "someNotificationTextBody")
.AddContent(new NotificationContent(IdentityAddress.Parse("did:e:prod.enmeshed.eu:dids:1a7063b5d2c7a8945bf43d"), DevicePushIdentifier.New(),
Expand All @@ -33,8 +33,8 @@ public void Built_message_has_all_properties_set()

message.Data["content-available"].Should().Be("1");

message.Android.CollapseKey.Should().Be("1");
message.Android.Notification.Tag.Should().Be("1");
message.Android.CollapseKey.Should().Be("testNotificationId");
message.Android.Notification.Tag.Should().Be("testNotificationId");
}

[Fact]
Expand Down