• Blog
  • How to implement Push notifications in Delphi

How to implement Push notifications in Delphi

Features of TNotificationCenter in Delphi

Publish date:
Discover more of what matters to you

Push actually means a transfer of some message from a server (provider) to a client (user). Push notifications can be compared with sending a message from an addresser that is a server to an addressee that is a user. The Push technology became popular thanks to the PointCast project that was working in the 1990s. The product used this technology for delivering news, stock market updates and other data. After that, there were trials to implement the Push technology in such browsers as Netscape Navigator and Internet Explorer but due to the low internet connection speed that we could observe at that time, the technology didn’t gain wide adoption.  And only after 2010 the Push notification technology again started gaining popularity but this time on mobile platforms.

Four components are used for the Push notification realization:

  1. A Push notification server where a notification itself is created;
  2. A server of an app author that sends a notification to a push notification server;
  3. A service of the client’s operating system that communicates with a notification server;
  4. An app itself that shows a push notification to a user.

As a rule, push notifications are informational messages for users/groups of users of an app. For example, banks usually inform their clients about crediting funds to an account  via push notifications.

Specificity of the realization based on the app’s operating system

As a rule, the base code for creating push notifications depending on the operating system that will be used for running an FMX app remains the same. But there is one peculiarity related to the notification server because each operating system has its own server. For iOS, it is APNS. For Android, it is Firebase Cloud Messaging. For Windows10/Mobile, it is WNS. Thanks to an event model of work of the notification component TNotificationCenter, we do not need to introduce any additional compiler directives or code branching. In all operating systems that have a capacity to display notifications, TNotificationCenter will work in the same way.

It’s also worth mentioning that the TNotificationCenter component has the same peculiarities:

  • Pending push notification;
  • Cancelation of a pending push notification;
  • Immediate creation of a notification;
  • Cancelation of the entire notification queue

Let’s consider such a notion as a pending push notification in more detail. For example, we need to notify a user that he/she has to drink a glass of water at 18:00. To do that we can use a public property Firedate that allows setting a date and time for creating a notification.

For example:

12345678910111213141516
procedure SetNotification(const aMessage: string);
begin
var vNotifiCenter:= TNotificationCenter.Create(nil);
try
var vNotification:= vNotifiCenter.CreateNotification;
try
vNotification.AlertBody:= aMessage;
vNotification.FireDate:= Now;
vNotifiCenter.PresentNotification(vNotification);
finally
vNotification.Free;
end;
finally
vNotifiCenter.Free;
end;
end;

To make this example work, do not forget to:

  • Add System.Notification into a uses module;
  • Call the code the following way: SetNotification (‘Notification sample’)

Also please pay attention to the fact that we will create all the necessary components inside SetNotification that’s why we do not need to add any additional elements to the form.

Now let’s have a look at the example in more detail. Here we can say that vNotification.FireDate:= Now. It means that we are planning to send a notification Now, just immediately. It means that if we change the value of vNotification.FireDate, we can change the date and time when the notification will be shown.

Other examples of work with TNotificationCenter will be considered below.

Data resources for Push notifications

The typical realization for Push notifications for the Android operating system is registration at the https://firebase.google.com/ service and further usage of Push notifications via Firebase. And for other operating systems – via their notification servers.

It’s worth mentioning the following facts:

  • RAD Studio 11.0 contains a component TPushEvents (it was available in earlier versions as well) that allows redefining/subscribing for your own notification server or blocking events of the current notification server thanks to your own events.
  • For TNotificationCenter, it is absolutely not obligatory to have a notification server. With the help of  TNotificationCenter, you can create a notification manually. A notification service is required only for background work and getting notifications only when our app is not running.

Now when we know the basic theory related to the introduction and use of Push notifications, let’s have a closer look at the tools for working with them offered by the IDE  RAD Studio 11.0.

Example of the Push notification app realization

Above we’ve already considered a basic example of creating notifications for users. For the Android operating system, it looked the following way:

In this example, the PushExample text looks not informative. Let’s improve this situation. To achieve this aim, let’s upgrade our procedure and add the following code into it:

1234567891011121314151617181920
procedure SetNotification(const aTitle, aMessage: string);
begin
var vNotifiCenter:= TNotificationCenter.Create(nil);
try
if vNotifiCenter.Supported then
begin
var vNotification:= vNotifiCenter.CreateNotification;
try
vNotification.AlertBody:= aMessage;
vNotification.Title:= aTitle;
vNotification.EnableSound:= true;
vNotifiCenter.PresentNotification(vNotification);
finally
vNotification.Free;
end;
end;
finally
vNotifiCenter.Free;
end;
end;

For your convenience, we’ve published a fully modified SetNotification procedure above. What has changed?

We’ve added an input parameter aTitle. It is the title of our notification:

  • We’ve added a condition if vNotifiCenter.Supported then begin that will check whether a notification system is available to us on the current operating system and only if yes, it will create a notification;
  • We’ve deleted vNotification.FireDate:= Now as we do not have a task to create a pending notification.

To demonstrate our work with a TNotificationCenter component, we offer to create a range of additional methods:

  • Cancelation of a planned notification;
  • Cancelation of all notifications

For doing that, let’s modify our example. We need to create a separate class that will be responsible for the realization of all the necessary methods. A full code of the proposed class can be found below:

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
unit Classes.MyNotification;
interface
uses
System.Notification;
type
TMyNotification = class
private
fNotifiCenter: TNotificationCenter;
public
procedure SetNotification(const aTitle, aMessage: string);
procedure SetFireDateNotification(const aName, aTitle, aMessage: string; aTime: TDateTime);
procedure CancelNotification(const aNotificationName: string);
procedure CancelAllNotification();
constructor Create();
destructor Destroy; override;
end;
implementation
{ TMyNotification }
procedure TMyNotification.CancelAllNotification;
begin
fNotifiCenter.CancelAll;
end;
procedure TMyNotification.CancelNotification(const aNotificationName: string);
begin
fNotifiCenter.CancelNotification(aNotificationName);
end;
constructor TMyNotification.Create();
begin
fNotifiCenter:= TNotificationCenter.Create(Nil);
end;
destructor TMyNotification.Destroy;
begin
fNotifiCenter.Free;
end;
procedure TMyNotification.SetFireDateNotification(const aName, aTitle,
aMessage: string; aTime: TDateTime);
begin
if fNotifiCenter.Supported then
begin
var vNotification:= fNotifiCenter.CreateNotification;
try
vNotification.Name:= aName;
vNotification.AlertBody:= aMessage;
vNotification.Title:= aTitle;
vNotification.EnableSound:= true;
vNotification.FireDate:= aTime;
fNotifiCenter.PresentNotification(vNotification);
finally
vNotification.Free;
end;
end;
end;
procedure TMyNotification.SetNotification(const aTitle, aMessage: string);
begin
if fNotifiCenter.Supported then
begin
var vNotification:= fNotifiCenter.CreateNotification;
try
vNotification.AlertBody:= aMessage;
vNotification.Title:= aTitle;
vNotification.EnableSound:= true;
fNotifiCenter.PresentNotification(vNotification);
finally
vNotification.Free;
end;
end;
end;
end.

We’ve created a class that has four core methods:

  • procedure SetNotification(const aTitle, aMessage: string);
  • procedure SetFireDateNotification(const aName, aTitle, aMessage: string; aTime: TDateTime);
  • procedure CancelNotification(const aNotificationName: string);
  • procedure CancelAllNotification();

As before, the SetNotification method sends notifications immediately.

The SetFireDateNotification method gives a name to a notification that is being created and sends it when FireDate comes.

The CancelNotification method cancels notifications, the search for a notification is based on its name.

The CancelAllNotification method cancels all notifications that were created.

We are always happy to provide you with quality Delphi software development services.

Subscribe to our newsletter and get amazing content right in your inbox.

This field is required
This field is required Invalid email address

Thank you for subscribing!
See you soon... in your inbox!

confirm your subscription, make sure to check your promotions/spam folder

Tags

Subscribe to our newsletter and get amazing content right in your inbox.

You can unsubscribe from the newsletter at any time

This field is required
This field is required Invalid email address

You're almost there...

A confirmation was sent to your email

confirm your subscription, make sure to check
your promotions/spam folder