Skip to content

Commit

Permalink
Merge pull request #17 from notifo-io/fix-tracking
Browse files Browse the repository at this point in the history
Fix tracking.
  • Loading branch information
SebastianStehle authored Jan 13, 2023
2 parents eff22a8 + d9bc554 commit cc77f79
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 10 deletions.
21 changes: 21 additions & 0 deletions sdk/Notifo.SDK/Extensions/TaskExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@ namespace Notifo.SDK.Extensions;

internal static class TaskExtensions
{
private static readonly Action<Task> IgnoreTaskContinuation = t => { var ignored = t.Exception; };

public static void Forget(this Task task)
{
if (task.IsCompleted)
{
#pragma warning disable IDE0059 // Unnecessary assignment of a value
var ignored = task.Exception;
#pragma warning restore IDE0059 // Unnecessary assignment of a value
}
else
{
task.ContinueWith(
IgnoreTaskContinuation,
CancellationToken.None,
TaskContinuationOptions.OnlyOnFaulted |
TaskContinuationOptions.ExecuteSynchronously,
TaskScheduler.Default);
}
}

public static async Task WithCancellation(this Task task, CancellationToken cancellationToken)
{
var tcs = new TaskCompletionSource<bool>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ partial void SetupPlatform()
OnNotificationReceived += PushEventsProvider_OnNotificationReceivedAndroid;
}

private void PushEventsProvider_OnNotificationReceivedAndroid(object sender, NotificationEventArgs e)
{
if (!string.IsNullOrWhiteSpace(e.Notification.TrackSeenUrl))
{
_ = TrackNotificationsAsync(e.Notification);
}
}

/// <inheritdoc />
public INotifoMobilePush SetNotificationHandler(INotificationHandler? notificationHandler)
{
Expand All @@ -50,6 +42,11 @@ public INotifoMobilePush SetImageCacheCapacity(int capacity)
return this;
}

private void PushEventsProvider_OnNotificationReceivedAndroid(object sender, NotificationEventArgs e)
{
TrackNotificationsAsync(e.Notification).Forget();
}

/// <inheritdoc />
public async Task OnBuildNotificationAsync(NotificationCompat.Builder notificationBuilder, UserNotificationDto notification)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Threading;
using System.Threading.Tasks;
using Notifo.SDK.CommandQueue;
using Notifo.SDK.Extensions;
using Notifo.SDK.Helpers;
using Notifo.SDK.PushEventProvider;
using Notifo.SDK.Resources;
Expand Down Expand Up @@ -196,7 +197,7 @@ async Task RegisterAsync()
Register(tokenToRegister);
}

_ = RegisterAsync();
RegisterAsync().Forget();
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public async Task DidReceivePullRefreshRequestAsync()
// iOS does not maintain a queue of undelivered notifications, therefore we have to query here.
var notifications = await GetPendingNotificationsAsync(refreshOptions.Take, refreshOptions.Period, default);

List<UserNotificationDto>? trackImmediatly = null;

foreach (var notification in notifications)
{
if (refreshOptions.RaiseEvent)
Expand All @@ -67,13 +69,18 @@ public async Task DidReceivePullRefreshRequestAsync()

if (notification.Silent || !refreshOptions.PresentNotification)
{
trackImmediatly ??= new List<UserNotificationDto>();
trackImmediatly.Add(notification);
continue;
}

await ShowLocalNotificationAsync(notification);
}

await TrackNotificationsAsync(notifications.ToArray());
if (trackImmediatly != null)
{
await TrackNotificationsAsync(trackImmediatly.ToArray());
}
}

private async Task<IEnumerable<UserNotificationDto>> GetPendingNotificationsAsync(int take, TimeSpan maxAge,
Expand Down Expand Up @@ -160,6 +167,10 @@ private async Task ShowLocalNotificationAsync(UserNotificationDto notification)
{
NotifoIO.Current.RaiseError(error.LocalizedDescription, null, this);
}
else
{
TrackNotificationsAsync(notification).Forget();
}
});
}

Expand Down

0 comments on commit cc77f79

Please sign in to comment.