FMX.SendCancelNotification Sample
This sample demonstrates how to send and cancel notifications, and the use of the TNotification and TNotificationCenter classes.
Contents
Location
You can find the SendCancelNotification sample project at:
- Start | Programs | Embarcadero RAD Studio Athens | Samples and then navigate to:
Object Pascal\Mobile Snippets\Notifications\SendCancelNotification
CPP\Mobile Snippets\Notifications\SendCancelNotification
- Subversion Repository:
Description
The SendCancelNotification sample demonstrates how to send and cancel notifications, and the use of the TNotification and TNotificationCenter classes.
With this sample, you can do the following:
- Send notifications immediately.
- Send scheduled notifications.
- Cancel scheduled notifications.
- Cancel all notifications.
How to Use the Sample
- Navigate to one of the locations given above and open:
- Delphi: SendCancelNotifications.dproj.
- C++: SendCancelNotifications.cbproj.
- Select the target platform, iOS, Android, and OS X supported.
- Press F9 or choose Run > Run.
- Interact with the sample:
- Click Send Scheduled Notifications to schedule a notification to be fired in 10 seconds.
- Click Send Notification Immediately to send a notification immediately.
- Click Cancel Scheduled to cancel the scheduled notification.
- Click Cancel All Notifications to cancel all notifications.
Files
File in Delphi | File in C++ | Contains |
---|---|---|
SendCancelNotifications.dproj |
SendCancelNotifications.cbproj |
The project itself. |
uMain.fmx |
uMain.fmx |
The main form where the components are located. |
uMain.pas |
SendCancelNotificationsPCH1.h, SendCancelNotificationsPCH1.cpp |
Used to define and implement the sample. |
Information.txt |
Information.txt |
Further information about the sample. |
Implementation
The TNotificationCenter component is the notification center; this component creates the notification.
The TNotification component is the notification itself.
Notification Details
After creating the notification, the sample sets the following notification details:
- The Name field uniquely identifies a notification.
- The AlertBody field adds the text of the notification.
Scheduling Notifications
FireDate is used to set the date and time when the notification is going to be fired.
- Immediate Notifications: The sample sets the FireDate to Now for notifications that need to be fired immediately.
- Scheduled Notifications: To fire the notification in 10 seconds, the sample sets the FireDate to the current system time and then uses EncodeTime to add 10 seconds to the current time.
- Warning: Windows does not support ScheduleNotification.
Firing Notifications
After adding the information for the notification and adding the scheduling information, the sample uses the following sentence to send the notification to the notification center.
Delphi:
NotificationC.ScheduleNotification(Notification);
C++:
NotificationC->ScheduleNotification(Notification);
The following sentence is equivalent for immediate notifications, and you may also use PresentNotification
to send notifications to the notification center when they have to be fired immediately.
Delphi:
NotificationC.PresentNotification(Notification);
C++:
NotificationC->PresentNotification(Notification);
- Tip: Windows does not support scheduled notifications, you can present immediate notifications in Windows with
PresentNotification
.
Cancelling Notifications
The sample uses CancelNotification to cancel a scheduled notification indicating the Name of the notification.
The sample uses CancelAll to cancels all notifications:
- The notifications that are scheduled will not be fired.
- The notifications, that belong to this application, and are available in the notification center or notification drawer are also cancelled and removed.
Uses
- System.Notification.TNotificationCenter
- System.Notification.TCustomNotificationCenter.CreateNotification
- System.Notification.TCustomNotificationCenter.ScheduleNotification
- System.Notification.TCustomNotificationCenter.PresentNotification
- System.Notification.TCustomNotificationCenter.CancelNotification
- System.Notification.TCustomNotificationCenter.CancelAll
- System.Notification.TNotification
- System.Notification.TNotification.Name.
- System.Notification.TNotification.AlertBody
- System.Notification.TNotification.FireDate
See Also
- Using Notifications
- Mobile Tutorial: Using Notifications (iOS and Android)
- Using the macOS Notification Center
Samples
- FireMonkey Android Notification Service sample
- FireMonkey Set Reset Badge Number sample
- OSX Dock Badges (Delphi)
- FireMonkey Notification Mac (Delphi)
- FireMonkey Notification Mac (C++)