VCL.Windows 10 Notifications Sample

From RAD Studio Code Examples
Jump to: navigation, search

This demo is a VCL application that shows how to use the TNotificationCenter component to create, present, and cancel notifications on Windows 8 and later Windows versions.

Location

You can find the Windows 10 Notifications sample project at:

Description

This sample demonstrates how to create, present, and cancel notifications on Windows 8 or later.

The sample uses a TNotificationCenter component.

How to Use the Sample

  1. Navigate to the location given above, and open:
    1. Delphi: NotificationProject.dproj
    2. C++: Notifications.cbproj
  2. Press F9 or choose Run > Run.
  3. Click the different buttons to create and delete notifications:
  • Click Show to present a notification.
  • Click Show Another to present a second notification.
  • Click Cancel to dismiss the notification presented after clicking Show.
  • Click Cancel Another to dismiss the notification presented after clicking Show Another.
  • Click Cancel All to cancel all the notifications.
  • Handle the notifications to show a received message in the TMemo.

Implementation

The sample uses the TNotificationCenter component to create and handle notifications. When a notification is presented in Windows, it appears as a banner in the right up/bottom side of the window.

If the notification is not handled by the user, it goes to the Action Center (Only for Windows 10).

The sample uses the buttons Cancel, Cancel Another, and Cancel All to remove notifications from the Action Center.

When the notification is handled, that is when a user clicks it, the OnReceiveLocalNotification event triggers and a line is added to the TMemo.

'Show' Button

It creates a notification named 'Windows10Notification'.

The OnClick event creates and defines the notification:

For Delphi:

procedure TNotificationsForm.btnShowClick(Sender: TObject);
var
  MyNotification: TNotification; //Defines a TNotification variable
begin
  MyNotification := NotificationCenter1.CreateNotification; //Creates the notification
  try
    MyNotification.Name := 'Windows10Notification'; //Defines the name of the notification.
    MyNotification.Title := 'Windows 10 Notification #1'; //Defines the name that appears when the notification is presented.
    MyNotification.AlertBody := 'RAD Studio 10 Seattle'; //Defines the body of the notification that appears below the title.

    NotificationCenter1.PresentNotification(MyNotification); //Presents the notification on the screen.
  finally
    MyNotification.Free; //Frees the variable
  end;
end;

For C++:

void __fastcall TForm1::btnShowClick(TObject *Sender)
{
  TNotification *MyNotification; 
  MyNotification = new TNotification; 
  try
  {
    MyNotification->Name = "Windows10Notification";  //Defines the name of the notification.
    MyNotification->Title = "Windows 10 Notification #1"; //Defines the name that appears when the notification is presented.
    MyNotification->AlertBody = "RAD Studio 10 Seattle"; //Defines the body of the notification that appears below the title.

    NotificationCenter1->PresentNotification(MyNotification); //Presents the notification on the screen.
  }
  __finally
  {
    delete MyNotification; 
  }
}

'Show Another' Button

The OnClick event creates and defines a second notification, similar to the first one, named 'Windows10Notification2'.

'Cancel' Button

The OnClick event cancels the notification presented with Show.

To cancel a specific notification, you provide the name as an argument to the CancelNotification method.

For Delphi:

procedure TNotificationsForm.btnCancelClick(Sender: TObject);
begin
  NotificationCenter1.CancelNotification('Windows10Notification'); //Cancels the 'Windows10Notification' notification.
end;

For C++:

void __fastcall TForm1::btnCancelClick(TObject *Sender)
{
    NotificationCenter1->CancelNotification("Windows10Notification"); //Cancels the 'Windows10Notification' notification.
}

'Cancel Another' Button

The OnClick event cancels the 'Windows10Notification2' notification presented with Show Another.

'Cancel All' Button

The OnClick event uses the CancelAll method to cancel all notifications at once:

For Delphi:

procedure TNotificationsForm.btnCancelAllClick(Sender: TObject);
begin
  NotificationCenter1.CancelAll; // Cancels all notifications.
end;

For C++:

void __fastcall TForm1::btnCancelAllClick(TObject *Sender)
{
  NotificationCenter1->CancelAll();
}

TMemo

The OnReceiveLocalNotification of the TNotificationCenter is defined to show a line when a notification is handled by the user. Handling a notification means that the user clicks it.

For Delphi:

procedure TNotificationsForm.NotificationCenter1ReceiveLocalNotification(Sender: TObject;
  ANotification: TNotification);
begin
  mmLog.Lines.Add('Notification received: ' + ANotification.Name); //Shows the name of the notification handled.
end;

For C++:

void __fastcall TForm1::NotificationCenter1ReceiveLocalNotification(TObject *Sender, TNotification *ANotification)
{
  mmLog->Lines->Add("Notification received: " + ANotification->Name); //Shows the name of the notification handled.
}

Uses

See Also