Kennisbank

Native Windows notifications met Delphi

Windows 10 en 11 hebben een nieuw notifications systeem. In een recente Delphi-versie is het component TNotificationCenter toegevoegd. Voeg dit toe aan je form en je kunt native Windows-notifications weergeven.

Voorbeeld:

const
  UPDATE_NOTIFICATION_ID = 1;

procedure TForm1.Button1Click;
var
  Notification: TNotification;
begin
  Notification := NotificationCenter1.CreateNotification;

  Notification.Name := 'MyName';
  Notification.Number := UPDATE_NOTIFICATION_ID;
  Notification.AlertBody := 'Update 2.3.5 is available, click here to install the update now.';
  Notification.EnableSound := False;
  Notification.FireDate := Now;
  Notification.HasAction := True;

  NotificationCenter1.PresentNotification(Notification);
end;

Om een klik op een melding af te handelen, implementeren we OnReceiveLocalNotification

procedure TForm1.NotificationCenter1ReceiveLocalNotification(Sender: TObject; ANotification: TNotification);
begin
  if ANotification.Number = UPDATE_NOTIFICATION_ID  then
  begin
    InstallUpdate;
  end;
end;

 

Om een melding te plannen, gebruiken we NotificationCenter.ScheduleNotification.

procedure ScheduleNotification;
var
  Notification: TNotification;
begin
  Notification := NotificationCenter1.CreateNotification;
  Notification.AlertBody := 'It''s time to drink coffee :-)';
  Notification.FireDate  := IncHour(Now);
  
  NotificationCenter1.ScheduleNotification(Notification);
end;

 
en om de melding(en) te annuleren:

procedure CancelNotifications;
begin
  // Cancel a specific one
  NotificationCenter1.CancelNotification('MyName');
  
  // Cancel all notifications
  NotificationCenter1.CancelAll;
end;

Geschreven door David Paul van der Wulp
Delphi ontwikkelaar

Contact

Laat ons helpen jouw ambities concreet te maken.