Windows Service com notificação (Delhi)

08/07/2021

0

Estou fazendo um serviço em delphi que após capturar um evento do banco de dados envia uma notificação, o mesmo código funciona quando se faz como uma aplicação, mas não quando é um serviço. Alguém saberia dizer o porquê e como resolver?

unit SRVMonitorBugUnit;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.SvcMgr, Vcl.Dialogs,
FireDAC.Phys.Intf, FireDAC.Stan.Option, FireDAC.Stan.Intf, FireDAC.Stan.Error,
FireDAC.UI.Intf, FireDAC.Stan.Def, FireDAC.Stan.Pool, FireDAC.Stan.Async,
FireDAC.Phys, FireDAC.Phys.FB, FireDAC.Phys.FBDef, FireDAC.VCLUI.Wait,
Data.DB, FireDAC.Comp.Client, System.Notification;

type
TSRVMonitorBug = class(TService)
FDConexaoMonitor: TFDConnection;
EAMonitor: TFDEventAlerter;
NotificacaoMonitor: TNotificationCenter;
procedure ServiceExecute(Sender: TService);
procedure EAMonitorAlert(ASender: TFDCustomEventAlerter;
const AEventName: string; const AArgument: Variant);


private
{ Private declarations }
public
function GetServiceController: TServiceController; override;
{ Public declarations }
end;

var
SRVMonitorBug: TSRVMonitorBug;

implementation

{$R *.dfm}

procedure ServiceController(CtrlCode: DWord); stdcall;
begin
SRVMonitorBug.Controller(CtrlCode);
end;

procedure TSRVMonitorBug.EAMonitorAlert(ASender: TFDCustomEventAlerter;
const AEventName: string; const AArgument: Variant);
var
MyNotification: TNotification;
begin
if (AEventName = 'Update_Safra') then
begin
MyNotification := NotificacaoMonitor.CreateNotification;
try
MyNotification.Name := 'Windows10Notification';
MyNotification.Title := 'Teste';
MyNotification.AlertBody := 'Teste';

NotificacaoMonitor.PresentNotification(MyNotification);
finally
MyNotification.Free;
end;
end;
end;

function TSRVMonitorBug.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;

procedure TSRVMonitorBug.ServiceExecute(Sender: TService); // Para que o serviço funcione continuamente até que seja interrompido pelo usuário
begin
while not Terminated do
ServiceThread.ProcessRequests(true);

end;

end.

Usei os componentes TFDConnection, TFDEventsAlerter e TNotificationCenter

OBS: Quando olho as notificações e ações do Windows o meu serviço não aparece entre os remetente
S. Dias

S. Dias

Responder

Assista grátis a nossa aula inaugural

Assitir aula

Saiba por que programar é uma questão de
sobrevivência e como aprender sem riscos

Assistir agora

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar