Delphi / FMX + iOS:UNUserNotificationCenter不会引发委托事件(本地通知)

时间:2018-10-07 10:33:01

标签: ios delphi notifications firemonkey unusernotificationcenter

我正在尝试使用Delphi / Firemonkey重现UNUserNotificationCenter(iOS10 +)。该应用已成功授权接收通知,并按预期显示所有本地通知,但UNUserNotificationCenter不会触发委托事件(例如,当用户点击通知项时)。

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;

我的委托对象:

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;

应分别在出现通知和应用处于前台或用户点击通知时分别引发以下事件。但是什么也没发生。

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;

我在做什么错? 谢谢!

0 个答案:

没有答案