Delphi/FMX + iOS: UNUserNotificationCenter don’t raises the delegate events (local notifications)

  

I’m trying to reproduce the UNUserNotificationCenter (iOS10+) with Delphi/Firemonkey. The app successfully authorizes for receiving notifications and show all local notifications as expected, nowever UNUserNotificationCenter doesn’t trigger the delegate events (e.g. when user taps on the notification item).

type
UNUserNotificationCenterDelegate = interface(IObjectiveC)
[‘{817A2C54-2F1E-4322-8BDB-2E77F8FAB1AE}’]
procedure willPresentNotification(center: UNUserNotificationCenter; notification: UNNotification; completionHandler: TUserNotificationsWithCompletionHandler1); cdecl;
procedure didReceiveNotificationResponse(center: UNUserNotificationCenter; response: UNNotificationResponse; completionHandler: TUserNotificationsWithCompletionHandler2); cdecl;
end;

My delegate object:

type
TUserNotificationCenterDelegate = class(TOCLocal, UNUserNotificationCenterDelegate)
public
procedure willPresentNotification(center: UNUserNotificationCenter; notification: UNNotification; completionHandler: TUserNotificationsWithCompletionHandler1); cdecl;
procedure didReceiveNotificationResponse(center: UNUserNotificationCenter; response: UNNotificationResponse; completionHandler: TUserNotificationsWithCompletionHandler2); cdecl;
end;

var
NotificationCenterDelegate: TUserNotificationCenterDelegate;

implementation

[…]

function TfrmApplication.AppEventHandler(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
begin
Result := true;

case AAppEvent of
TApplicationEvent.FinishedLaunching:
begin
UserNotificationCenterDelegate := TUserNotificationCenterDelegate.Create;
TUNUserNotificationCenter.OCClass.currentNotificationCenter.setDelegate((UserNotificationCenterDelegate as ILocalObject).GetObjectID);
TUNUserNotificationCenter.OCClass.currentNotificationCenter.requestAuthorizationWithOptions(UNAuthorizationOptionAlert + UNAuthorizationOptionSound, nil);
end;
TApplicationEvent.EnteredBackground:
begin
DEF_APPLICATION_IN_BACKGROUND := true;
end;
TApplicationEvent.BecameActive:
begin
DEF_APPLICATION_IN_BACKGROUND := false;
TUNUserNotificationCenter.OCClass.currentNotificationCenter.removeAllDeliveredNotifications;
end;
else
Result := false;
end;
end;

The events below should to be raised every time by when notification presents and the app is in foreground or when user taps the notification, respectively. But nothing happens.

procedure TUserNotificationCenterDelegate.willPresentNotification(center: UNUserNotificationCenter; notification: UNNotification; completionHandler: TUserNotificationsWithCompletionHandler1); cdecl;
var
aImp: procedure(options: NSUInteger); cdecl;
aOptions: UNNotificationPresentationOptions;
begin
WriteLog(AP_LOG_HEADER + ‘Will present notification’);
//completionHandler
@aImp := imp_implementationWithBlock(completionHandler);
aOptions := UNNotificationPresentationOptionAlert;
aImp(aOptions);
imp_removeBlock(@aImp);
end;

procedure TUserNotificationCenterDelegate.didReceiveNotificationResponse(center: UNUserNotificationCenter; response: UNNotificationResponse; completionHandler: TUserNotificationsWithCompletionHandler2); cdecl;
var
aImp: procedure(); cdecl;
begin
WriteLog(AP_LOG_HEADER + ‘Receive notification response’);
//completionHandler
@aImp := imp_implementationWithBlock(completionHandler);
aImp();
imp_removeBlock(@aImp);
end;

What am I doing wrong?
Thanks!

Comments are closed.