Skip to content

Commit

Permalink
work on ios notification delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
aritchie committed Aug 14, 2019
1 parent f0ab95d commit fac5012
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 33 deletions.
23 changes: 1 addition & 22 deletions src/Shiny.Notifications/Platforms/iOS/NotificationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public Task<IEnumerable<Notification>> GetPending()
.GetPendingNotificationRequestsAsync();

var notifications = requests
.Select(this.FromNative)
.Select(x => x.FromNative())
.Where(x => x != null);
tcs.TrySetResult(notifications);
}
Expand Down Expand Up @@ -159,27 +159,6 @@ protected Task Invoke(Action action)
});
return tcs.Task;
}


protected virtual Notification FromNative(UNNotificationRequest native)
{
if (!Int32.TryParse(native.Identifier, out var i))
return null;

var shiny = new Notification
{
Id = i,
Title = native.Content?.Title,
Message = native.Content?.Body,
Sound = native.Content.Sound?.ToString(),
//Metadata = native.Content.UserInfo.FromNsDictionary()
};

if (native.Trigger is UNCalendarNotificationTrigger calendar)
shiny.ScheduleDate = calendar.NextTriggerDate.ToDateTime();

return shiny;
}
}
}
//UNUserNotificationCenter.Current.SetNotificationCategories(
Expand Down
24 changes: 21 additions & 3 deletions src/Shiny.Notifications/Platforms/iOS/PlatformExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
using System;
using System.Collections.Generic;
using Foundation;
using UserNotifications;


namespace Shiny.Notifications
{
static class PlatformExtensions
public static class PlatformExtensions
{
public static Notification FromNative(this UNNotificationRequest native)
{
if (!Int32.TryParse(native.Identifier, out var i))
return null;

var shiny = new Notification
{
Id = i,
Title = native.Content?.Title,
Message = native.Content?.Body,
Sound = native.Content.Sound?.ToString(),
//Metadata = native.Content.UserInfo.FromNsDictionary()
};

if (native.Trigger is UNCalendarNotificationTrigger calendar)
shiny.ScheduleDate = calendar.NextTriggerDate?.ToDateTime() ?? DateTime.Now;
// if null, it is firing and it is firing right now

return shiny;
}
}
}
27 changes: 19 additions & 8 deletions src/Shiny.Notifications/Platforms/iOS/ShinyNotificationDelegate.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Threading.Tasks;
using Shiny.Logging;
using UserNotifications;


Expand All @@ -8,21 +10,30 @@ public class ShinyNotificationDelegate : UNUserNotificationCenterDelegate
{
public override async void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
{
var processor = ShinyHost.Resolve<NotificationProcessor>();
if (processor != null)
await processor.Entry(response.Notification.Request.Identifier);

await this.Execute(response.Notification.Request, (not, del) => del.OnEntry(not));
completionHandler();
}


public override async void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
var processor = ShinyHost.Resolve<NotificationProcessor>();
if (processor != null)
await processor.Receive(notification.Request.Identifier);

await this.Execute(notification.Request, (not, del) => del.OnReceived(not));
completionHandler(UNNotificationPresentationOptions.Alert);
}


async Task Execute(UNNotificationRequest request, Func<Notification, INotificationDelegate, Task> execute)
{
try
{
var not = request.FromNative();
var del = ShinyHost.Resolve<INotificationDelegate>();
await execute(not, del);
}
catch (Exception ex)
{
Log.Write(ex);
}
}
}
}

0 comments on commit fac5012

Please sign in to comment.