Skip to content

Commit

Permalink
Add raw payload and send out a native ApplePushNotification
Browse files Browse the repository at this point in the history
  • Loading branch information
aritchie committed Jun 11, 2024
1 parent 7ca0425 commit 38a6194
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
15 changes: 3 additions & 12 deletions src/Shiny.Push/Platforms/Apple/ApplePushNotification.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
using System.Collections.Generic;
using Foundation;

namespace Shiny.Push;

/// <summary>
/// The "native" data for iOS in a strongly typed package
/// </summary>
/// <param name="Data">A dictionary of the custom payload data</param>
/// <param name="Aps">NULL if received in the background</param>
/// <param name="Notification">The generic notification details - NULL if in the background</param>
public record ApplePushNotification(
IDictionary<string, string> Data,
Aps? Aps,
NSDictionary? RawPayload,
Notification? Notification
)
: PushNotification(
Data,
Notification
);

public record Aps(IDictionary<string, string> AdditionalPayloadData, int? Badge, ApsAlert? Alert, ApsSound? Sound, bool? ContentAvailable);
public record ApsAlert(string Title, string Body);
public record ApsSound(bool Critical, string Name, double? Volume);
);
14 changes: 7 additions & 7 deletions src/Shiny.Push/Platforms/Apple/PushManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class PushManager(
IIosLifecycle.IRemoteNotifications,
IIosLifecycle.INotificationHandler
{
static readonly NSString apsKey = new NSString("aps");
static readonly NSString alertKey = new NSString("alert");
static readonly NSString apsKey = new("aps");
static readonly NSString alertKey = new("alert");

TaskCompletionSource<NSData>? tokenSource;

Expand Down Expand Up @@ -246,19 +246,19 @@ public void OnDidReceive(NSDictionary userInfo, Action<UIBackgroundFetchResult>
{
var fetchResult = services
.GetServices<IPushDelegate>()
.Select(x =>
.Select(y =>
{
try
{
return (x as IApplePushDelegate)?.GetFetchResult(data);
return (y as IApplePushDelegate)?.GetFetchResult(data);
}
catch (Exception ex)
{
logger.LogError(ex, $"Error executing ApplePushDelegate {x.GetType().FullName}.GetFetchResult");
return null;
}
})
.FirstOrDefault(x => x != null);
.FirstOrDefault(y => y != null);
platform.InvokeOnMainThread(
() => completionHandler.Invoke(fetchResult ?? UIBackgroundFetchResult.NewData)
Expand Down Expand Up @@ -304,7 +304,7 @@ protected virtual void TryProcessIncomingNotification(UNNotification? notificati
}


protected virtual PushNotification ToPushNotification(UNNotification notification)
protected virtual ApplePushNotification ToPushNotification(UNNotification notification)
{
var c = notification.Request.Content;
var shinyNotification = new Notification(
Expand All @@ -313,7 +313,7 @@ protected virtual PushNotification ToPushNotification(UNNotification notificatio
);

var dict = c.UserInfo?.FromNsDictionary() ?? new Dictionary<string, string>(0);
var data = new PushNotification(dict, shinyNotification);
var data = new ApplePushNotification(dict, c.UserInfo, shinyNotification);

return data;
}
Expand Down

0 comments on commit 38a6194

Please sign in to comment.