Skip to content

Commit edc8143

Browse files
Merge branch 'master' of github.com:notifo-io/sdk-xamarin
# Conflicts: # sdk/Notifo.SDK/NotifoMobilePush/NotifoMobilePushImplementation.tracking.cs
2 parents 0acf10b + 30e2cc8 commit edc8143

11 files changed

+86
-73
lines changed

sample/Sample.iOS.Shared/Properties/AssemblyInfo.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Reflection;
2-
using System.Runtime.CompilerServices;
32
using System.Runtime.InteropServices;
43

54
// General Information about an assembly is controlled through the following

sample/Sample/NotifoStartup.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
using Notifo.SDK;
99
using Notifo.SDK.FirebasePlugin;
10-
using Notifo.SDK.NotifoMobilePush;
11-
using Notifo.SDK.PushEventProvider;
1210
using Xamarin.Forms;
1311

1412
namespace Sample

sdk/Notifo.SDK/CommandQueue/ICommand.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// All rights reserved. Licensed under the MIT license.
66
// ==========================================================================
77

8-
using System;
98
using System.Threading;
109
using System.Threading.Tasks;
1110

sdk/Notifo.SDK/INotifoMobilePush.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ public partial interface INotifoMobilePush : INotifoClient
5757
/// <returns>The current instance.</returns>
5858
INotifoMobilePush SetPushEventsProvider(IPushEventsProvider pushEventsProvider);
5959

60+
/// <summary>
61+
/// Raises an error.
62+
/// </summary>
63+
/// <param name="error">The error message.</param>
64+
/// <param name="exception">The exception.</param>
65+
/// <param name="source">The source of the error.</param>
66+
void RaiseError(string error, Exception? exception, object? source);
67+
6068
/// <summary>
6169
/// Register for notifications on demand.
6270
/// </summary>

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
using Java.Net;
1414
using Microsoft.Extensions.Caching.Memory;
1515
using Notifo.SDK.Resources;
16-
using Serilog;
1716

1817
namespace Notifo.SDK.NotifoMobilePush
1918
{
@@ -118,7 +117,7 @@ internal void OnBuildNotification(NotificationCompat.Builder notificationBuilder
118117
}
119118
catch (Exception ex)
120119
{
121-
Log.Error(Strings.DownloadImageError, ex);
120+
NotifoIO.Current.RaiseError(Strings.DownloadImageError, ex, this);
122121
}
123122

124123
return null;

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

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
using System.Net.Http;
1313
using System.Threading.Tasks;
1414
using Foundation;
15-
using Microsoft.Extensions.Caching.Memory;
1615
using Notifo.SDK.Extensions;
1716
using Notifo.SDK.Resources;
1817
using Serilog;
@@ -23,7 +22,6 @@ namespace Notifo.SDK.NotifoMobilePush
2322
{
2423
internal partial class NotifoMobilePushImplementation : NSObject
2524
{
26-
private readonly IMemoryCache imageCache = new MemoryCache(new MemoryCacheOptions());
2725
private HttpClient httpClient;
2826
private INotificationHandler? notificationHandler;
2927

@@ -113,7 +111,7 @@ bool IsRecent(DateTimeOffset date)
113111
}
114112
catch (Exception ex)
115113
{
116-
Log.Error(Strings.NotificationsRetrieveException, ex);
114+
NotifoIO.Current.RaiseError(Strings.NotificationsRetrieveException, ex, this);
117115
}
118116

119117
return Array.Empty<UserNotificationDto>();
@@ -137,7 +135,7 @@ private async Task ShowLocalNotificationAsync(UserNotificationDto notification)
137135
{
138136
if (error != null)
139137
{
140-
Log.Debug(error.LocalizedDescription);
138+
NotifoIO.Current.RaiseError(error.LocalizedDescription, null, this);
141139
}
142140
});
143141
}
@@ -154,43 +152,7 @@ private async Task<UNMutableNotificationContent> EnrichNotificationContentAsync(
154152
content.Body = notification.Body;
155153
}
156154

157-
var image = string.IsNullOrWhiteSpace(notification.ImageLarge) ? notification.ImageSmall : notification.ImageLarge;
158-
159-
if (!string.IsNullOrWhiteSpace(image))
160-
{
161-
var imagePath = await GetImageAsync(image);
162-
163-
if (!string.IsNullOrWhiteSpace(imagePath))
164-
{
165-
var attachmentName = $"{Guid.NewGuid()}{Path.GetExtension(imagePath)}";
166-
var attachmentUrl = new NSUrl(attachmentName, NSFileManager.DefaultManager.GetTemporaryDirectory());
167-
168-
// TODO: We copy the image twice. Really weird.
169-
NSFileManager.DefaultManager.Copy(NSUrl.FromFilename(imagePath), attachmentUrl, out var error);
170-
171-
if (error != null)
172-
{
173-
// TODO: Expose via error event.
174-
Log.Error(error.LocalizedDescription);
175-
}
176-
177-
var attachement = UNNotificationAttachment.FromIdentifier(
178-
Constants.ImageLargeKey,
179-
attachmentUrl,
180-
new UNNotificationAttachmentOptions(),
181-
out error);
182-
183-
if (error == null)
184-
{
185-
content.Attachments = new UNNotificationAttachment[] { attachement };
186-
}
187-
else
188-
{
189-
// TODO: Expose via error event.
190-
Log.Error(error.LocalizedDescription);
191-
}
192-
}
193-
}
155+
await AddImageAsync(content, notification);
194156

195157
var actions = new List<UNNotificationAction>();
196158

@@ -265,6 +227,49 @@ private async Task<UNMutableNotificationContent> EnrichNotificationContentAsync(
265227
return content;
266228
}
267229

230+
private async Task AddImageAsync(UNMutableNotificationContent content, UserNotificationDto notification)
231+
{
232+
var image = string.IsNullOrWhiteSpace(notification.ImageLarge) ? notification.ImageSmall : notification.ImageLarge;
233+
234+
if (string.IsNullOrWhiteSpace(image))
235+
{
236+
return;
237+
}
238+
239+
var imagePath = await GetImageAsync(image);
240+
241+
if (string.IsNullOrWhiteSpace(imagePath))
242+
{
243+
return;
244+
}
245+
246+
var attachmentName = $"{Guid.NewGuid()}{Path.GetExtension(imagePath)}";
247+
var attachmentUrl = new NSUrl(attachmentName, NSFileManager.DefaultManager.GetTemporaryDirectory());
248+
249+
// TODO: We copy the image twice. Really weird.
250+
NSFileManager.DefaultManager.Copy(NSUrl.FromFilename(imagePath), attachmentUrl, out var error);
251+
252+
if (error != null)
253+
{
254+
NotifoIO.Current.RaiseError(error.LocalizedDescription, null, this);
255+
return;
256+
}
257+
258+
var attachement = UNNotificationAttachment.FromIdentifier(
259+
Constants.ImageLargeKey,
260+
attachmentUrl,
261+
new UNNotificationAttachmentOptions(),
262+
out error);
263+
264+
if (error != null)
265+
{
266+
NotifoIO.Current.RaiseError(error.LocalizedDescription, null, this);
267+
return;
268+
}
269+
270+
content.Attachments = new UNNotificationAttachment[] { attachement };
271+
}
272+
268273
public void DidReceiveNotificationResponse(UNNotificationResponse response)
269274
{
270275
var userInfo = response.Notification.Request.Content.UserInfo.ToDictionary();
@@ -289,19 +294,18 @@ public void DidReceiveNotificationResponse(UNNotificationResponse response)
289294
}
290295
}
291296

292-
private async Task<string> GetImageAsync(string imageUrl)
297+
private async Task<string?> GetImageAsync(string imageUrl)
293298
{
294299
try
295300
{
296-
// TODO: Not really sure if the dictionary provides any value at all.
297-
if (imageCache.TryGetValue(imageUrl, out string imagePath) && File.Exists(imagePath))
301+
// Use the base64 value of the URL.
302+
var imagePath = Path.Combine(FileSystem.CacheDirectory, imageUrl.ToBase64());
303+
304+
if (File.Exists(imagePath))
298305
{
299306
return imagePath;
300307
}
301308

302-
// Use the base64 value of the URL.
303-
imagePath = Path.Combine(FileSystem.CacheDirectory, imageUrl.ToBase64());
304-
305309
// Copy directly from the web stream to the image stream to reduce memory allocations.
306310
using (var fileStream = new FileStream(imagePath, FileMode.Open))
307311
{
@@ -311,15 +315,14 @@ private async Task<string> GetImageAsync(string imageUrl)
311315
}
312316
}
313317

314-
imageCache.Set(imageUrl, imagePath);
315-
316318
return imagePath;
317319
}
318320
catch (Exception ex)
319321
{
320-
Log.Error(Strings.DownloadImageError, ex);
321-
return string.Empty;
322+
NotifoIO.Current.RaiseError(Strings.DownloadImageError, ex, this);
322323
}
324+
325+
return null;
323326
}
324327
}
325328
}

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Net.Http;
1010
using Notifo.SDK.CommandQueue;
1111
using Notifo.SDK.PushEventProvider;
12+
using Serilog;
1213

1314
namespace Notifo.SDK.NotifoMobilePush
1415
{
@@ -85,22 +86,30 @@ public NotifoMobilePushImplementation(Func<HttpClient> httpClientFactory, ISeenN
8586
this.clientProvider = new NotifoClientProvider(httpClientFactory);
8687

8788
SetupPlatform();
89+
90+
OnError += (sender, args) =>
91+
{
92+
Log.Error(args.Error, args.Exception);
93+
};
8894
}
8995

9096
partial void SetupPlatform();
9197

98+
/// <inheritdoc/>
9299
public INotifoMobilePush SetApiKey(string apiKey)
93100
{
94101
clientProvider.ApiKey = apiKey;
95102
return this;
96103
}
97104

105+
/// <inheritdoc/>
98106
public INotifoMobilePush SetBaseUrl(string baseUrl)
99107
{
100108
clientProvider.ApiUrl = baseUrl;
101109
return this;
102110
}
103111

112+
/// <inheritdoc/>
104113
public INotifoMobilePush SetPushEventsProvider(IPushEventsProvider pushEventsProvider)
105114
{
106115
if (this.pushEventsProvider == pushEventsProvider)
@@ -131,6 +140,13 @@ public INotifoMobilePush SetPushEventsProvider(IPushEventsProvider pushEventsPro
131140
return this;
132141
}
133142

143+
/// <inheritdoc/>
144+
public void RaiseError(string error, Exception? exception, object? source)
145+
{
146+
OnError?.Invoke(this, new NotificationErrorEventArgs(error, exception, source));
147+
}
148+
149+
/// <inheritdoc/>
134150
public void Register()
135151
{
136152
if (string.IsNullOrEmpty(token))
@@ -141,6 +157,7 @@ public void Register()
141157
commandQueue.Run(new TokenRegisterCommand { Token = token });
142158
}
143159

160+
/// <inheritdoc/>
144161
public void Unregister()
145162
{
146163
if (string.IsNullOrEmpty(token))
@@ -189,10 +206,5 @@ private void UpdateToken(string newToken)
189206
Register();
190207
}
191208
}
192-
193-
public void RaiseError(string error, Exception? exception, object? source)
194-
{
195-
OnError?.Invoke(this, new NotificationErrorEventArgs(error, exception, source));
196-
}
197209
}
198210
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
using System.Threading.Tasks;
1313
using Notifo.SDK.Helpers;
1414
using Notifo.SDK.Resources;
15-
using Serilog;
1615

1716
namespace Notifo.SDK.NotifoMobilePush
1817
{
@@ -33,7 +32,7 @@ public async Task<HashSet<Guid>> GetSeenNotificationsAsync()
3332
}
3433
catch (Exception ex)
3534
{
36-
Log.Error(Strings.TrackingException, ex);
35+
NotifoIO.Current.RaiseError(Strings.TrackingException, ex, this);
3736
return new HashSet<Guid>();
3837
}
3938
finally
@@ -65,7 +64,8 @@ public async Task TrackNotificationsAsync(params Guid[] ids)
6564
}
6665
catch (Exception ex)
6766
{
68-
Log.Error(Strings.TrackingException, ex);
67+
NotifoIO.Current.RaiseError(Strings.TrackingException, ex, this);
68+
return;
6969
}
7070
finally
7171
{

sdk/Notifo.SDK/NotifoMobilePush/TokenRegisterCommand.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ public async ValueTask ExecuteAsync(
5151
}
5252
catch (Exception ex)
5353
{
54-
((NotifoMobilePushImplementation)NotifoIO.Current).RaiseError(ex.Message, ex, this);
55-
56-
Log.Error(ex, Strings.TokenRefreshFailException);
54+
NotifoIO.Current.RaiseError(Strings.TokenRefreshFailException, ex, this);
5755
throw ex;
5856
}
5957
finally

sdk/Notifo.SDK/NotifoMobilePush/TokenUnregisterCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public async ValueTask ExecuteAsync(
2525
}
2626
catch (Exception ex)
2727
{
28-
((NotifoMobilePushImplementation)NotifoIO.Current).RaiseError(ex.Message, ex, this);
28+
NotifoIO.Current.RaiseError(ex.Message, ex, this);
2929
throw ex;
3030
}
3131
}

0 commit comments

Comments
 (0)