FMX.Notification.Mac (C++)

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 - C++ > 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, RepeatedNotification, CancelScheduled, and CancelAll.
      • The Text properties of the buttons to Scheduled Notification, Notification Immediately, Repeat 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.

void __fastcall TDemonstration::ScheduleNotificationClick(TObject *Sender)
{
	if (NotificationCenter1->Supported()) {
		TNotification *myNotification = NotificationCenter1->CreateNotification();
		__try {
			myNotification->Name = "MyNotification";
			myNotification->AlertBody = "RAD Studio Alexandria is here!";
			// Fire in 10 seconds
			myNotification->FireDate = Now() + EncodeTime(0, 0, 10, 0);
			// Send notification to the Notification Center
			NotificationCenter1->ScheduleNotification(myNotification);
		}
		__finally {
			myNotification->DisposeOf();
                }

	}
}

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

void __fastcall TDemonstration::PresentNotificationClick(TObject *Sender)
{
  if (NotificationCenter1->Supported()) {
	   TNotification *myNotification = NotificationCenter1->CreateNotification();
           __try {
			   myNotification->Name = "MyNotification";
			   myNotification->AlertBody = "RAD Studio Alexandria is here!";
		   // Send notification to the Notification Center
	           NotificationCenter1->PresentNotification(myNotification);
          }
          __finally {
			   myNotification->DisposeOf();
          }
 }
}

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

void __fastcall TDemonstration::RepeatNotificationClick(TObject *Sender)
{
	if (NotificationCenter1->Supported()) {
		TNotification *myNotification = NotificationCenter1->CreateNotification();
		__try {
			myNotification->Name = "MyNotification";
			myNotification->AlertBody = "RAD Studio Alexandria is here!";
			// Fire in 10 seconds
			myNotification->FireDate = Now() + EncodeTime(0, 0, 10, 0);
			// Repeated each minute
			myNotification->RepeatInterval = TRepeatInterval::Minute;
			// Send notification to the Notification Center
			NotificationCenter1->ScheduleNotification(myNotification);
		}
		__finally {
			myNotification->DisposeOf();
                }

	}
}

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

void __fastcall TDemonstration::CancelScheduleClick(TObject *Sender)
{
	NotificationCenter1->CancelNotification("MyNotification");
	ShowMessage("Schedule Notifications cancelled");
}

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

void __fastcall TDemonstration::CancelAllClick(TObject *Sender)
{
	NotificationCenter1->CancelAll();
	ShowMessage("All Notifications canceled");
}

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