已经用Delphi 11.3做了一个VCL应用程序。
如果用户在屏幕右下角最初可见(已弹出)我的通知时单击我的通知,则 TNotificationCenter 的 OnReceiveLocalNotification 事件可以正常工作(触发)。但是,如果用户没有快速反应并且通知进入 Windows 操作中心(就像通知在一段时间后执行的那样),那么当用户单击那里的通知时,事件不会被触发。至少这是我的 Windows 11 PC 上发生的情况。
这是预期的行为,还是这里出了问题,如果是,是什么?
我希望在用户单击通知时收到通知(只要我的应用程序仍在运行),无论通知当前显示在何处。
重现问题的代码:
unit MainUnit;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.Notification;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure ReceiveLocalNotification(Sender: TObject;
ANotification: TNotification);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
NotificationCenter1: TNotificationCenter;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
N: TNotification;
begin
N := NotificationCenter1.CreateNotification;
try
N.Title := 'My Title';
N.AlertBody := 'My alert body';
NotificationCenter1.PresentNotification(N);
finally
N.Free;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
NotificationCenter1 := TNotificationCenter.Create(Self);
NotificationCenter1.OnReceiveLocalNotification := ReceiveLocalNotification;
end;
procedure TForm1.ReceiveLocalNotification(Sender: TObject;
ANotification: TNotification);
begin
ShowMessage('I received the notification clicked event');
end;
end.
按照指定使用通知
单击 macOS、iOS 或 Android 中的通知,会将发送通知的应用程序置于最前面,无论该应用程序是在后台运行还是完全关闭。
用户在 Windows 中单击通知时没有特殊行为。
通知消失后,只有 Windows 不再向生成通知的应用程序发送通知消息。