FMX.Notification.Mac (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example shows you how to use the TNotificationCenter component to implement notifications for your OS X applications using the OS X Notification Center.

To build and test this example:

  1. Create a Multi-Device application: File > New > Multi-Device Application - Delphi > Blank Application.
  2. Add the following components to your form:
    • Five TButton controls. In the Object Inspector, set the following properties:
      • The Name properties of the buttons to ScheduleNotification, Notification, RepeateNotification, CancelScheduled, and CancelAll.
      • The Text properties of the buttons to Scheduled Notification, Notification Immediately, Repeated Notification, Cancel Scheduled, and Cancel All.
    • A TNotificationCenter control. This component allows you to easily access the Notification Service.
At this point, your form should look like this:

Notifications Demo.png

Code

Add the following code to the OnClick event handler for the ScheduleNotification.

procedure TDemonstration.ScheduleNotificationClick(Sender: TObject);
var
  MyNotification: TNotification;
begin
   // Create an instance of TNotification
  MyNotification := NotificationCenter1.CreateNotification;
  try
    MyNotification.Name := 'MyNotification';
    MyNotification.AlertBody := 'RAD Studio Sydney is here!';
    // Fired in 5 seconds
    MyNotification.FireDate := Now + EncodeTime(0, 0, 5, 0);
    // Note: You must send the notification to '''Notification Center''' to be displayed.
    NotificationCenter1.ScheduleNotification(MyNotification);
  finally
    MyNotification.DisposeOf;
  end;
end;

Add the following code to the OnClick event handler for the Notification.

procedure TDemonstration.NotificationClick(Sender: TObject);
var
  MyNotification: TNotification;
begin
   // Create an instance of TNotification
  MyNotification := NotificationCenter1.CreateNotification;
  try
    MyNotification.Name := 'MyNotification';
    MyNotification.AlertBody := 'RAD Studio Sydney is here now!';
    // Note: You must send the notification to '''Notification Center''' to be displayed.
    NotificationCenter1.PresentNotification(MyNotification);
  finally
    MyNotification.DisposeOf;
  end;
end;

Add the following code to the OnClick event handler for the RepeatNotification.

procedure TDemonstration.RepeatNotificationClick(Sender: TObject);
var
  MyNotification: TNotification;
begin
   // Create an instance of TNotification
  MyNotification := NotificationCenter1.CreateNotification;
  try
    MyNotification.Name := 'MyNotification';
    MyNotification.AlertBody := 'RAD Studio Sydney is here!';
    // Fired in 5 seconds
    MyNotification.FireDate := Now + EncodeTime(0, 0, 5, 0);
	//Repeated each minute
    MyNotification.RepeatInterval := TRepeatInterval.Minute;
    // Note: You must send the notification to '''Notification Center''' to be displayed.
    NotificationCenter1.ScheduleNotification(MyNotification);
  finally
    MyNotification.DisposeOf;
  end;
end;

Add the following code to the OnClick event handler for the CancelScheduled.

procedure TDemonstration.CancelScheduledClick(Sender: TObject);
begin
  NotificationCenter1.CancelNotification('MyNotification');
  ShowMessage('Schedule Notifications cancelled');
end;

Add the following code to the OnClick event handler for the CancelAll.

procedure TDemonstration.CancelAllClick(Sender: TObject);
begin
  Notificationcenter1.CancelAll;
  ShowMessage('All Notifications canceled');
end;

You will get notification banners, unless you change the appropriate setting in System Settings > Notifications to get alerts.

Banner:

Notification Banner.png

Alert:

Notification Alert.png

Notification Center:

Notification Center.png

Uses

See Also