Skip to content

Commit 2a8eb69

Browse files
Several fixes and comments.
1 parent 0db2563 commit 2a8eb69

File tree

5 files changed

+179
-15
lines changed

5 files changed

+179
-15
lines changed

sdk/Notifo.SDK/Extensions/StringExtensions.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
// ==========================================================================
77

88
using System;
9+
using System.Globalization;
10+
using System.Security.Cryptography;
911
using System.Text;
1012

1113
namespace Notifo.SDK.Extensions
@@ -16,5 +18,70 @@ public static string ToBase64(this string value)
1618
{
1719
return Convert.ToBase64String(Encoding.UTF8.GetBytes(value));
1820
}
21+
22+
public static string Sha256(this string value)
23+
{
24+
using (var hash = SHA256.Create())
25+
{
26+
var bytesString = Encoding.UTF8.GetBytes(value);
27+
var bytesHash = hash.ComputeHash(bytesString);
28+
29+
var builder = new StringBuilder();
30+
31+
foreach (byte b in bytesHash)
32+
{
33+
builder.Append(b.ToString("x2"));
34+
}
35+
36+
return builder.ToString();
37+
}
38+
}
39+
40+
public static string AppendQueries(this string url, string? key1, object? value1)
41+
{
42+
return AppendQueries(url, key1, value1, null!, null!);
43+
}
44+
45+
public static string AppendQueries(this string url, string? key1, object? value1, string? key2, object? value2)
46+
{
47+
if (string.IsNullOrWhiteSpace(url))
48+
{
49+
return url;
50+
}
51+
52+
var builder = new StringBuilder(url);
53+
54+
var hasQuery = url.Contains('?', StringComparison.OrdinalIgnoreCase);
55+
56+
void Append(string? key, object? value)
57+
{
58+
if (string.IsNullOrWhiteSpace(key) || value == null)
59+
{
60+
return;
61+
}
62+
63+
var valueString = string.Format(CultureInfo.InvariantCulture, "{0}", value);
64+
65+
if (hasQuery)
66+
{
67+
builder.Append('&');
68+
}
69+
else
70+
{
71+
builder.Append('?');
72+
}
73+
74+
builder.Append(key);
75+
builder.Append('=');
76+
builder.Append(Uri.EscapeDataString(valueString));
77+
78+
hasQuery = true;
79+
}
80+
81+
Append(key1, value1);
82+
Append(key2, value2);
83+
84+
return builder.ToString();
85+
}
1986
}
2087
}

sdk/Notifo.SDK/NotifoMobilePush/NotifoMobilePushImplementation.android.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using AndroidX.Core.App;
1313
using Java.Net;
1414
using Microsoft.Extensions.Caching.Memory;
15+
using Notifo.SDK.Extensions;
1516
using Notifo.SDK.Resources;
1617

1718
namespace Notifo.SDK.NotifoMobilePush
@@ -91,14 +92,12 @@ internal void OnBuildNotification(NotificationCompat.Builder notificationBuilder
9192
notificationHandler?.OnBuildNotification(notificationBuilder, notification);
9293
}
9394

94-
private Bitmap? GetBitmap(string bitmapUrl, int requestWidth = -1, int requestHeight = -1)
95+
private Bitmap? GetBitmap(string bitmapUrl, int width = -1, int height = -1)
9596
{
9697
try
9798
{
98-
if (requestWidth > 0 && requestHeight > 0)
99-
{
100-
bitmapUrl = $"{bitmapUrl}?width={requestWidth}&height={requestHeight}";
101-
}
99+
// Let the server resize the image to the perfect format.
100+
bitmapUrl = bitmapUrl.AppendQueries("width", width, "height", height);
102101

103102
if (bitmapCache.TryGetValue(bitmapUrl, out Bitmap cachedBitmap))
104103
{

sdk/Notifo.SDK/NotifoMobilePush/NotifoMobilePushImplementation.ios.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,10 @@ public async Task DidReceiveNotificationRequestAsync(UNNotificationRequest reque
4242

4343
var notification = new UserNotificationDto().FromDictionary(request.Content.UserInfo.ToDictionary());
4444

45-
if (!string.IsNullOrWhiteSpace(notification.TrackSeenUrl))
46-
{
47-
await TrackNotificationsAsync(notification);
48-
}
49-
5045
await EnrichNotificationContentAsync(bestAttemptContent, notification);
46+
47+
// Always track our notifications as seen.
48+
await TrackNotificationsAsync(notification);
5149
}
5250

5351
public async Task DidReceivePullRefreshRequestAsync(PullRefreshOptions? options = null)
@@ -60,8 +58,7 @@ public async Task DidReceivePullRefreshRequestAsync(PullRefreshOptions? options
6058
{
6159
if (options.RaiseEvent)
6260
{
63-
var eventArgs = new NotificationEventArgs(notification);
64-
OnReceived(eventArgs);
61+
OnReceived(new NotificationEventArgs(notification));
6562
}
6663

6764
if (notification.Silent)
@@ -239,6 +236,7 @@ private async Task AddImageAsync(UNMutableNotificationContent content, UserNotif
239236
return;
240237
}
241238

239+
// The system needs a file extension here, but it actually does not matter if the extension matchs to the actual format.
242240
var attachmentName = $"{Guid.NewGuid()}.png";
243241
var attachmentUrl = new NSUrl(attachmentName, NSFileManager.DefaultManager.GetTemporaryDirectory());
244242

@@ -294,14 +292,17 @@ public void DidReceiveNotificationResponse(UNNotificationResponse response)
294292
{
295293
try
296294
{
297-
// Use the base64 value of the URL.
298-
var imagePath = Path.Combine(FileSystem.CacheDirectory, imageUrl.ToBase64());
295+
// Convert the hash of the url to create short files names. Base64 will create longer file names.
296+
var imagePath = Path.Combine(FileSystem.CacheDirectory, imageUrl.Sha256());
299297

300298
if (File.Exists(imagePath))
301299
{
302300
return imagePath;
303301
}
304302

303+
// Let the server decide how the image should be delivered.
304+
imageUrl = imageUrl.AppendQueries("preset", "MobileIOS");
305+
305306
// Copy directly from the web stream to the image stream to reduce memory allocations.
306307
using (var fileStream = new FileStream(imagePath, FileMode.Create))
307308
{

sdk/Notifo.SDK/NotifoMobilePush/TrackSeenCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ public async ValueTask ExecuteAsync(
2828
{
2929
var trackUserNotificationDto = new TrackNotificationDto
3030
{
31+
DeviceIdentifier = Token,
32+
// Track all notifications at once.
3133
Seen = Ids.Select(x => x.ToString()).ToList(),
32-
DeviceIdentifier = Token
3334
};
3435

3536
await NotifoIO.Current.Notifications.ConfirmMeAsync(trackUserNotificationDto, ct);
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// ==========================================================================
2+
// Notifo.io
3+
// ==========================================================================
4+
// Copyright (c) Sebastian Stehle
5+
// All rights reserved. Licensed under the MIT license.
6+
// ==========================================================================
7+
8+
using System.Linq;
9+
using Notifo.SDK.Extensions;
10+
using Xunit;
11+
12+
namespace Notifo.SDK.UnitTests
13+
{
14+
public class StringExtensionsTests
15+
{
16+
[Fact]
17+
public void Should_calculate_base64()
18+
{
19+
var input = "https://notifo.io";
20+
21+
var result = input.ToBase64();
22+
23+
Assert.NotNull(result);
24+
}
25+
26+
[Fact]
27+
public void Should_calculate_sha256()
28+
{
29+
var input = "https://notifo.io";
30+
31+
var result = input.Sha256();
32+
33+
Assert.True(result.All(c => (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')));
34+
}
35+
36+
[Fact]
37+
public void Should_append_query_to_empty_query()
38+
{
39+
var input = "https://notifo.io";
40+
41+
var result = input.AppendQueries("q1", "1");
42+
43+
Assert.Equal("https://notifo.io?q1=1", result);
44+
}
45+
46+
[Fact]
47+
public void Should_append_query_to_existing_query()
48+
{
49+
var input = "https://notifo.io?key=value";
50+
51+
var result = input.AppendQueries("q1", "1");
52+
53+
Assert.Equal("https://notifo.io?key=value&q1=1", result);
54+
}
55+
56+
[Fact]
57+
public void Should_append_queries_to_empty_query()
58+
{
59+
var input = "https://notifo.io";
60+
61+
var result = input.AppendQueries("q1", "1", "q2", "2");
62+
63+
Assert.Equal("https://notifo.io?q1=1&q2=2", result);
64+
}
65+
66+
[Fact]
67+
public void Should_append_queries_to_existing_query()
68+
{
69+
var input = "https://notifo.io?key=value";
70+
71+
var result = input.AppendQueries("q1", "1", "q2", "2");
72+
73+
Assert.Equal("https://notifo.io?key=value&q1=1&q2=2", result);
74+
}
75+
76+
[Fact]
77+
public void Should_not_append_null_values()
78+
{
79+
var input = "https://notifo.io?key=value";
80+
81+
var result = input.AppendQueries("q1", null, "q2", null);
82+
83+
Assert.Equal(input, result);
84+
}
85+
86+
[Fact]
87+
public void Should_not_append_null_keys()
88+
{
89+
var input = "https://notifo.io?key=value";
90+
91+
var result = input.AppendQueries(null, "1", null, "2");
92+
93+
Assert.Equal(input, result);
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)