Push notifications that goes through the extension are always received when app is killed, I can notice that on the device e through the logs printed by the extension. When app is in foreground, nothing happens, neither I see logs in the extension or in the WillPresentNotification method in the AppDelegate (method that works perfectly in foreground with "normal" push that do not need to be intercepted by the extension). I tried also to add the method ReceivedRemoteNotification(...), neither this method was called as I've seen from the logs.
I use a UNNotificationServiceExtension to map the content of the notification.
[Register("NotificationService")]
public class NotificationService : UNNotificationServiceExtension
{
Action<UNNotificationContent> ContentHandler { get; set; }
UNMutableNotificationContent BestAttemptContent { get; set; }
.
.
.
protected NotificationService(IntPtr handle) : base(handle)
{
// Note: this .ctor should not contain any initialization logic.
}
public override void DidReceiveNotificationRequest(UNNotificationRequest request,
Action<UNNotificationContent> contentHandler)
{
Console.WriteLine("NOTIFICATIONS: NotificationExtension: entered DidReceive");
ContentHandler = contentHandler;
BestAttemptContent = (UNMutableNotificationContent)request.Content.MutableCopy();
// Mapping of body and title
ContentHandler(BestAttemptContent);
}
public override void TimeWillExpire()
{
// Called just before the extension will be terminated by the system.
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
Console.WriteLine("NOTIFICATIONS: NotificationExtension: entered TimeWillExpire");
ContentHandler(BestAttemptContent);
}
}
In the AppDelegate I have the code to show the notification in foreground:
[Register("AppDelegate")]
public partial class AppDelegate : FormsApplicationDelegate,
IUNUserNotificationCenterDelegate,
IUIAlertViewDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary launchOptions)
{
LogBroker.Instance.TraceDebug("START");
.
.
.
LogBroker.Instance.TraceDebug("RegisterForNotifications: Register For Push Notification");
UNUserNotificationCenter.Current.RequestAuthorization(
UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound,
(approved, err) => {
if (err != null)
{
LogBroker.Instance.TraceError($"RegisterForNotifications: {err.LocalizedDescription}");
return;
}
if (approved)
{
InvokeOnMainThread(() =>
{
LogBroker.Instance.TraceDebug("RegisterForNotifications: Approved");
UIApplication.SharedApplication.RegisterForRemoteNotifications();
});
}
else
{
LogBroker.Instance.TraceWarning($"RegisterForNotifications: Rejected by user!");
}
});
UNUserNotificationCenter.Current.Delegate = this;
LoadApplication(new App());
return base.FinishedLaunching(app, launchOptions);
}
.
.
.
[Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
public void WillPresentNotification(UNUserNotificationCenter _, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
LogBroker.Instance.TraceDebug($"NOTIFICATIONS: entered WillPresentNotification");
var pushType = notification.Request.Content.UserInfo?.ValueForKey(new NSString("push-type"))?
.ToString() ?? "";
LogBroker.Instance.TraceDebug($"NOTIFICATIONS: push type - {pushType}");
if (pushType == "call")
{
completionHandler(UNNotificationPresentationOptions.None);
}
else
{
LogBroker.Instance.TraceDebug($"NOTIFICATIONS: show push notification");
completionHandler(UNNotificationPresentationOptions.Alert |
UNNotificationPresentationOptions.Sound);
}
}
}
The payload of the notifications is something like this:
{
aps = {
alert = {
"loc-args" = (
"***"
);
"loc-key" = "IM_MSG";
};
badge = 1;
"mutable-content" = 1;
sound = "msg.caf";
};
"call-id" = 909d32775168c0db;
"domain-name" = "***";
panda = blue;
"pn_ttl" = 30;
"push-services" = vdes;
"push-type" = message;
"text-message" = "DND;ON";
}