FMX.Notification.Mac (Object Pascal)

From Appmethod Code Examples
Jump to: navigation, search

Description

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

To build and test this example:

  1. Create a FireMonkey desktop application: File > New > FireMonkey Desktop Application - Object Pascal > HD FireMonkey Application.
  2. Add the following components to your form:
    • Four TButton controls; from the Object Inspector, set the Name properties of the buttons to ScheduleNotification, Notification, CancelScheduled, and CancelAll. To follow the example rename the Text property to Send Scheduled Notification, Send Notification Immediately, Cancel Scheduled, and Cancel All.
    • A TNotificationCenter control; from the Object Inspector. 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 := 'Delphi XE6 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 := 'Delphi XE6 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 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