This tutorial describes the basic steps to use notifications on your mobile device.

Three Basic Notification or Alert Styles

When users set notifications for apps on their mobile devices, notifications can be delivered from apps in the three basic styles shown here.
The banner appears briefly, but the alert requires dismissal by the user.

iOS Badge Number and Android Notification Number

iOS Badge Number

iPad

Android Notification Number

Android

Notification Banner on Mobile Devices

iOS

Android

Notification Center on Mobile Devices

The following images show the notification center on an iPad (Notification Center) and Android (notification drawer), where the user can pull down the list of all recent notifications.

iOS

Android

Access the Notification Service

On the mobile platforms, FireMonkey provides the TNotificationCenter component that allows you to easily access the Notification Service.

To access the notification service, do the following:

Create a new mobile application:

File > New > Multi-Device Application-Object Pascal

File > New > Multi-Device Application-Appmethod C++

For Object Pascal applications, add the following 2 units to the uses clause if they are not present:

usesFMX.Platform,FMX.Notification;

For C++ applications, add the following include operators to the project header file:

Note: In your source code, call the following methods in different OnClick events of TButton components.

Set the Icon Badge Number and Notification Number from Code

TNotification.Number defines the Icon Badge Number (for iOS devices) and the notification number (for Android devices).

To set the iOS icon badge number (for Object Pascal or C++) or the Android notification number (only for Object Pascal apps), you can implement the following methods:

Object Pascal:

procedureTForm1.SetNumber;varMyNotification:TNotification;begin// Create an instance of TNotificationMyNotification:=NotificationCenter1.CreateNotification;try// --- your code goes here ---// Set the icon or notification numberMyNotification.Number:=18;// Set the alert messageMyNotification.AlertBody:='Object Pascal for your mobile device is here!';// Note: You must send the notification to the notification center for the Icon Badge Number to be displayed.NotificationCenter1.PresentNotification(MyNotification);finallyMyNotification.DisposeOf;end;end;

After you set the Notification Message (AlertBody), you can see it at the top of your device Home Screen. For iOS devices, this message is similar to the following screen:

Update or Cancel a Scheduled Notification Message

Each Scheduled Notification Message is identified through the Name property of the TNotification object.

Note: For the iOS platform, CancelNotification and ScheduleNotification can cancel or update only those notifications that have not been presented yet. If a notification has already appeared in the Device Notification List, you can neither update nor cancel the notification on iOS.

To update a scheduled notification, simply call the ScheduleNotification method again with an instance of TNotificationCenter that has the same name (Name property).

To cancel a scheduled notification, you can simply call the CancelNotification method with the identifier you used:

Present the Notification Message Immediately

You can also show the notification message immediately through the PresentNotification function.

To show a notification message, you need to create an instance of the TNotification class, and then define the Name and AlertBody fields.

Object Pascal:

procedureTForm1.PresentNotification;varMyNotification:TNotification;beginMyNotification:=NotificationCenter1.CreateNotification;tryMyNotification.Name:='MyNotification';MyNotification.AlertBody:='Object Pascal for your mobile device is here!';// Set Icon Badge Number (for iOS) or message number (for Android) as wellMyNotification.Number:=18;MyNotification.EnableSound:=False;// Send message to the notification centerNotificationCenter1.PresentNotification(MyNotification);finallyMyNotification.DisposeOf;end;end;

C++:

void__fastcallTForm1::presentNotification(){if(NotificationCenter1->Supported()){TNotification*myNotification=NotificationCenter1->CreateNotification();__try{myNotification->Name="MyNotification";myNotification->AlertBody="C++ for your mobile device is here!";// Set Icon Badge Number (for iOS) or message number (for Android) as wellmyNotification->Number=18;myNotification->EnableSound=False;// Send notification to the notification centerNotificationCenter1->PresentNotification(myNotification);}__finally{myNotification->DisposeOf();}}}

Notification Banner or Notification Alert

By default, your application shows the notification banner:

Notification Banner on iPad

Notification Banner on Android devices

Android

Notification Alert (only for iOS devices)

To use a notification alert instead of a notification banner (only for iOS devices), the end user needs to change the Alert style to Alerts through the configuration page of Notification Center, available in the device Settings:

Add Action to the Notification Alert (iOS Only)

You can also customize an alert by adding an Action button that opens the application.

To customize an Alert Action, you need to set the AlertAction field to the Action button name, and then set the HasAction field to True, as follows.

void__fastcallTForm1::actionNotification(){if(NotificationCenter1->Supported()){TNotification*myNotification=NotificationCenter1->CreateNotification();__try{myNotification->Name="MyNotification";myNotification->AlertBody="C++ for iOS is here!";myNotification->Number=2;myNotification->AlertAction="Launch";myNotification->HasAction=True;myNotification->FireDate=Now()+EncodeTime(0,0,20,0);NotificationCenter1->ScheduleNotification(myNotification);}__finally{myNotification->DisposeOf();}}}

Note: Only iOS devices support the notification alert feature.

The notification alert opens at the time that was specified through the FireDate field.