To receive push notifications in Delphi, you can use the Firebase Cloud Messaging (FCM) service. FCM is a cross-platform messaging solution that allows you to send and receive notifications on your mobile devices, as well as send messages to other devices that are running your app.
Here is an example of how you can use FCM in Delphi to receive push notifications:
- Install the FirebaseMessaging package from the Delphi component library.
- In the uses clause of your Delphi unit, add Firebase.Messaging.Helpers.
- In the OnCreate event of your form, add the following code to initialize FCM and set up a listener for incoming notifications:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
procedure TForm1.FormCreate(Sender: TObject); begin // Initialize FCM if not TFirebaseMessaging.IsInitialized then TFirebaseMessaging.Initialize; // Set up a listener for incoming notifications TMessageManager.DefaultManager.SubscribeToMessage(TMessageReceivedNotification, procedure(const Sender: TObject; const M: TMessage) begin // Handle the incoming notification here end); end; |
In the listener function, you can handle the incoming notification by displaying an alert or updating the UI of your app. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
procedure TForm1.FormCreate(Sender: TObject); begin // Initialize FCM if not TFirebaseMessaging.IsInitialized then TFirebaseMessaging.Initialize; // Set up a listener for incoming notifications TMessageManager.DefaultManager.SubscribeToMessage(TMessageReceivedNotification, procedure(const Sender: TObject; const M: TMessage) var Notification: TMessageReceivedNotification; begin Notification := TMessageReceivedNotification(M); // Display the notification title and body ShowMessage(Notification.Title + ‘: ‘ + Notification.Body); end); end; |
You can also use other FCM features, such as subscribing to topic-based notifications or sending messages to specific devices, by using the methods and properties provided by the TFirebaseMessaging class. For more information, please refer to the Delphi documentation and the FCM documentation.
Leave a Reply