Skip to content

Commit cc77f79

Browse files
Merge pull request #17 from notifo-io/fix-tracking
Fix tracking.
2 parents eff22a8 + d9bc554 commit cc77f79

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
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: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,6 @@ partial void SetupPlatform()
2828
OnNotificationReceived += PushEventsProvider_OnNotificationReceivedAndroid;
2929
}
3030

31-
private void PushEventsProvider_OnNotificationReceivedAndroid(object sender, NotificationEventArgs e)
32-
{
33-
if (!string.IsNullOrWhiteSpace(e.Notification.TrackSeenUrl))
34-
{
35-
_ = TrackNotificationsAsync(e.Notification);
36-
}
37-
}
38-
3931
/// <inheritdoc />
4032
public INotifoMobilePush SetNotificationHandler(INotificationHandler? notificationHandler)
4133
{
@@ -50,6 +42,11 @@ public INotifoMobilePush SetImageCacheCapacity(int capacity)
5042
return this;
5143
}
5244

45+
private void PushEventsProvider_OnNotificationReceivedAndroid(object sender, NotificationEventArgs e)
46+
{
47+
TrackNotificationsAsync(e.Notification).Forget();
48+
}
49+
5350
/// <inheritdoc />
5451
public async Task OnBuildNotificationAsync(NotificationCompat.Builder notificationBuilder, UserNotificationDto notification)
5552
{

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: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public async Task DidReceivePullRefreshRequestAsync()
5858
// iOS does not maintain a queue of undelivered notifications, therefore we have to query here.
5959
var notifications = await GetPendingNotificationsAsync(refreshOptions.Take, refreshOptions.Period, default);
6060

61+
List<UserNotificationDto>? trackImmediatly = null;
62+
6163
foreach (var notification in notifications)
6264
{
6365
if (refreshOptions.RaiseEvent)
@@ -67,13 +69,18 @@ public async Task DidReceivePullRefreshRequestAsync()
6769

6870
if (notification.Silent || !refreshOptions.PresentNotification)
6971
{
72+
trackImmediatly ??= new List<UserNotificationDto>();
73+
trackImmediatly.Add(notification);
7074
continue;
7175
}
7276

7377
await ShowLocalNotificationAsync(notification);
7478
}
7579

76-
await TrackNotificationsAsync(notifications.ToArray());
80+
if (trackImmediatly != null)
81+
{
82+
await TrackNotificationsAsync(trackImmediatly.ToArray());
83+
}
7784
}
7885

7986
private async Task<IEnumerable<UserNotificationDto>> GetPendingNotificationsAsync(int take, TimeSpan maxAge,
@@ -160,6 +167,10 @@ private async Task ShowLocalNotificationAsync(UserNotificationDto notification)
160167
{
161168
NotifoIO.Current.RaiseError(error.LocalizedDescription, null, this);
162169
}
170+
else
171+
{
172+
TrackNotificationsAsync(notification).Forget();
173+
}
163174
});
164175
}
165176

0 commit comments

Comments
 (0)