Skip to content

Commit ecd9892

Browse files
Fix tracking.
1 parent 454ac73 commit ecd9892

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed

sdk/Notifo.SDK/Extensions/TaskExtensions.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,27 @@ namespace Notifo.SDK.Extensions;
1313

1414
internal static class TaskExtensions
1515
{
16+
private static readonly Action<Task> IgnoreTaskContinuation = t => { var ignored = t.Exception; };
17+
18+
public static void Forget(this Task task)
19+
{
20+
if (task.IsCompleted)
21+
{
22+
#pragma warning disable IDE0059 // Unnecessary assignment of a value
23+
var ignored = task.Exception;
24+
#pragma warning restore IDE0059 // Unnecessary assignment of a value
25+
}
26+
else
27+
{
28+
task.ContinueWith(
29+
IgnoreTaskContinuation,
30+
CancellationToken.None,
31+
TaskContinuationOptions.OnlyOnFaulted |
32+
TaskContinuationOptions.ExecuteSynchronously,
33+
TaskScheduler.Default);
34+
}
35+
}
36+
1637
public static async Task WithCancellation(this Task task, CancellationToken cancellationToken)
1738
{
1839
var tcs = new TaskCompletionSource<bool>();

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ public INotifoMobilePush SetImageCacheCapacity(int capacity)
4343

4444
private void PushEventsProvider_OnNotificationReceivedAndroid(object sender, NotificationEventArgs e)
4545
{
46-
if (!string.IsNullOrWhiteSpace(e.Notification.TrackSeenUrl))
47-
{
48-
_ = TrackNotificationsAsync(e.Notification);
49-
}
46+
TrackNotificationsAsync(e.Notification).Forget();
5047
}
5148

5249
internal void OnBuildNotification(NotificationCompat.Builder notificationBuilder, UserNotificationDto notification)

sdk/Notifo.SDK/NotifoMobilePush/NotifoMobilePushImplementation.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Threading;
1212
using System.Threading.Tasks;
1313
using Notifo.SDK.CommandQueue;
14+
using Notifo.SDK.Extensions;
1415
using Notifo.SDK.Helpers;
1516
using Notifo.SDK.PushEventProvider;
1617
using Notifo.SDK.Resources;
@@ -196,7 +197,7 @@ async Task RegisterAsync()
196197
Register(tokenToRegister);
197198
}
198199

199-
_ = RegisterAsync();
200+
RegisterAsync().Forget();
200201
}
201202

202203
/// <inheritdoc/>

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,29 @@ public async Task DidReceivePullRefreshRequestAsync(PullRefreshOptions? options
6060
// iOS does not maintain a queue of undelivered notifications, therefore we have to query here.
6161
var notifications = await GetPendingNotificationsAsync(options.Take, options.Period, default);
6262

63+
List<UserNotificationDto>? trackImmediatly = null;
64+
6365
foreach (var notification in notifications)
6466
{
6567
if (options.RaiseEvent)
6668
{
6769
OnReceived(new NotificationEventArgs(notification));
6870
}
6971

70-
if (notification.Silent)
72+
if (notification.Silent || !options.PresentNotification)
7173
{
74+
trackImmediatly ??= new List<UserNotificationDto>();
75+
trackImmediatly.Add(notification);
7276
continue;
7377
}
7478

75-
if (options.PresentNotification)
76-
{
77-
await ShowLocalNotificationAsync(notification);
78-
}
79+
await ShowLocalNotificationAsync(notification);
7980
}
8081

81-
await TrackNotificationsAsync(notifications.ToArray());
82+
if (trackImmediatly != null)
83+
{
84+
await TrackNotificationsAsync(notifications.ToArray());
85+
}
8286
}
8387

8488
private async Task<IEnumerable<UserNotificationDto>> GetPendingNotificationsAsync(int take, TimeSpan maxAge,
@@ -167,6 +171,10 @@ private async Task ShowLocalNotificationAsync(UserNotificationDto notification)
167171
{
168172
NotifoIO.Current.RaiseError(error.LocalizedDescription, null, this);
169173
}
174+
else
175+
{
176+
TrackNotificationsAsync(notification).Forget();
177+
}
170178
});
171179
}
172180

@@ -240,7 +248,7 @@ private async Task EnrichBasicAsync(UNMutableNotificationContent content, UserNo
240248
UNUserNotificationCenter.Current.SetNotificationCategories(new NSSet<UNNotificationCategory>(categories.ToArray()));
241249

242250
// Without this call action buttons will not be added or updated.
243-
_ = await UNUserNotificationCenter.Current.GetNotificationCategoriesAsync();
251+
await UNUserNotificationCenter.Current.GetNotificationCategoriesAsync();
244252

245253
content.CategoryIdentifier = categoryId;
246254
}

0 commit comments

Comments
 (0)