Delphi: TNotificationCenter vs TTrayIcon.ShowBalloonHint

ShowBalloonHint was built for Windows XP, Vista and 7. It displays notification in Windows 8, 8.1 and 10. However, fired notification events can’t be detected.

Instead, you can fire notifications using the code below: (It fires notification on Windows 8, 8.1, 10)

var
  Note: TNotification;
begin
  Note := Form1.NotificationCenter1.CreateNotification;
  try
    try
      Note.Name := 'Test-Note';
      Note.Title := 'Test-Notification';
      Note.AlertBody := 'Notification Test!';
      Note.FireDate := Now;
      Form1.NotificationCenter1.PresentNotification(Note);
    except
    end;
  finally
    Note.Free;
  end;
end;

We can detect if user clicked on the notification by using the code below:

procedure TForm1.NotificationCenter1ReceiveLocalNotification(Sender: TObject;
  ANotification: TNotification);
begin
  showmessage(ANotification.Title);
end;

Leave a comment