Skip to content

Commit efd985b

Browse files
Do enrichment in the right order.
1 parent fa8f975 commit efd985b

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

sdk/Notifo.SDK/INotifoMobilePush.ios.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public partial interface INotifoMobilePush
2222
/// Method for processing notification before delivery.
2323
/// </summary>
2424
/// <param name="request">The request that was received.</param>
25-
/// <param name="bestAttemptContent">The notification content.</param>
25+
/// <param name="content">The notification content to enrich.</param>
2626
/// <returns>A <see cref="Task"/> representing the result of the asynchronous operation.</returns>
27-
Task DidReceiveNotificationRequestAsync(UNNotificationRequest request, UNMutableNotificationContent bestAttemptContent);
27+
Task DidReceiveNotificationRequestAsync(UNNotificationRequest request, UNMutableNotificationContent content);
2828

2929
/// <summary>
3030
/// Method for pulling pending notifications.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ partial void SetupPlatform()
2727
OnNotificationReceived += PushEventsProvider_OnNotificationReceivedAndroid;
2828
}
2929

30+
/// <inheritdoc />
3031
public INotifoMobilePush SetNotificationHandler(INotificationHandler? notificationHandler)
3132
{
3233
this.notificationHandler = notificationHandler;
3334
return this;
3435
}
3536

37+
/// <inheritdoc />
3638
public INotifoMobilePush SetImageCacheCapacity(int capacity)
3739
{
3840
bitmapCache.EnsureCapacity(capacity);

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

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,36 @@ internal partial class NotifoMobilePushImplementation : NSObject
1717
{
1818
private INotificationHandler? notificationHandler;
1919

20+
/// <inheritdoc />
2021
public INotifoMobilePush SetNotificationHandler(INotificationHandler? notificationHandler)
2122
{
2223
this.notificationHandler = notificationHandler;
2324
return this;
2425
}
2526

26-
public async Task DidReceiveNotificationRequestAsync(UNNotificationRequest request, UNMutableNotificationContent bestAttemptContent)
27+
/// <inheritdoc />
28+
public async Task DidReceiveNotificationRequestAsync(UNNotificationRequest request, UNMutableNotificationContent content)
2729
{
2830
RaiseDebug(Strings.ReceivedNotification, this, request.Content.UserInfo);
2931

3032
var notification = new UserNotificationDto().FromDictionary(request.Content.UserInfo.ToDictionary());
3133

32-
await EnrichNotificationContentAsync(bestAttemptContent, notification);
34+
// We have ony limited time in the notification service, so we dot things in the right order.
3335

34-
// Always track our notifications as seen.
36+
// 1. Do the cheap enrichment first.
37+
await EnrichBasicAsync(content, notification);
38+
39+
// 2. Do the tracking to ensure that the request arrives.
3540
await TrackNotificationsAsync(notification);
41+
42+
// 3. Enrich with images, which need to be downloaded.
43+
await EnrichImagesAsync(content, notification);
44+
45+
// 4. Custom enrichment code (could be potentially be expensive).
46+
EnrichWithCustomCode(content, notification);
3647
}
3748

49+
/// <inheritdoc />
3850
public async Task DidReceivePullRefreshRequestAsync(PullRefreshOptions? options = null)
3951
{
4052
options ??= new PullRefreshOptions();
@@ -131,10 +143,15 @@ private void OnReceived(NotificationEventArgs eventArgs)
131143

132144
private async Task ShowLocalNotificationAsync(UserNotificationDto notification)
133145
{
134-
var content = new UNMutableNotificationContent();
146+
var content = new UNMutableNotificationContent
147+
{
148+
UserInfo = notification.ToDictionary().ToNSDictionary()
149+
};
150+
151+
await EnrichBasicAsync(content, notification);
152+
await EnrichImagesAsync(content, notification);
135153

136-
content = await EnrichNotificationContentAsync(content, notification);
137-
content.UserInfo = notification.ToDictionary().ToNSDictionary();
154+
EnrichWithCustomCode(content, notification);
138155

139156
var request = UNNotificationRequest.FromIdentifier(notification.Id.ToString(), content, trigger: null);
140157

@@ -147,7 +164,7 @@ private async Task ShowLocalNotificationAsync(UserNotificationDto notification)
147164
});
148165
}
149166

150-
private async Task<UNMutableNotificationContent> EnrichNotificationContentAsync(UNMutableNotificationContent content, UserNotificationDto notification)
167+
private async Task EnrichBasicAsync(UNMutableNotificationContent content, UserNotificationDto notification)
151168
{
152169
if (!string.IsNullOrWhiteSpace(notification.Subject))
153170
{
@@ -159,8 +176,6 @@ private async Task<UNMutableNotificationContent> EnrichNotificationContentAsync(
159176
content.Body = notification.Body;
160177
}
161178

162-
await AddImageAsync(content, notification);
163-
164179
var actions = new List<UNNotificationAction>();
165180

166181
if (!string.IsNullOrWhiteSpace(notification.ConfirmUrl) &&
@@ -226,12 +241,15 @@ private async Task<UNMutableNotificationContent> EnrichNotificationContentAsync(
226241

227242
content.Sound ??= UNNotificationSound.Default;
228243

229-
notificationHandler?.OnBuildNotification(content, notification);
244+
EnrichWithCustomCode(content, notification);
245+
}
230246

231-
return content;
247+
private void EnrichWithCustomCode(UNMutableNotificationContent content, UserNotificationDto notification)
248+
{
249+
notificationHandler?.OnBuildNotification(content, notification);
232250
}
233251

234-
private async Task AddImageAsync(UNMutableNotificationContent content, UserNotificationDto notification)
252+
private async Task EnrichImagesAsync(UNMutableNotificationContent content, UserNotificationDto notification)
235253
{
236254
var image = string.IsNullOrWhiteSpace(notification.ImageLarge) ? notification.ImageSmall : notification.ImageLarge;
237255

0 commit comments

Comments
 (0)