Delphi Android Firebase cloud messaging

  

I’m trying to get confidence with Android Firebase cloud messaging.
First I’ve registered my test application on the console.firebase.google.com as described here
After that I tried the code below, but the application raise exception at the line:
ServiceConnection.Active := True;

What I’m doing wrong?
The code:
unit Unit3;

interface

uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Memo.Types,
FMX.Controls.Presentation, FMX.ScrollBox, FMX.Memo,
System.PushNotification,
FMX.StdCtrls;

type
TForm3 = class(TForm)
MemoLog: TMemo;
Button1: TButton;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
FDeviceId: string;
FDeviceToken: string;
procedure OnServiceConnectionChange(Sender: TObject;
PushChanges: TPushService.TChanges);
procedure OnReceiveNotificationEvent(Sender: TObject;
const ServiceNotification: TPushServiceNotification);
public
{ Public declarations }
end;

var
Form3: TForm3;

implementation

{$R *.fmx}

procedure TForm3.OnServiceConnectionChange(Sender: TObject;
PushChanges: TPushService.TChanges);
var
PushService: TPushService;
begin
PushService :=
TPushServiceManager.Instance.GetServiceByName(TPushService.TServiceNames.GCM);
if TPushService.TChange.DeviceToken in PushChanges then
begin
FDeviceToken :=
PushService.DeviceTokenValue[TPushService.TDeviceTokenNames.DeviceToken];
MemoLog.Lines.Add(‘FireBase Token: ‘ + FDeviceToken);
Log.d(‘Firebase device token: token=’ + FDeviceToken);
end;
if (TPushService.TChange.Status in PushChanges) and (PushService.Status
= TPushService.TStatus.StartupError) then
MemoLog.Lines.Add(‘Error: ‘ + PushService.StartupError);
end;

procedure TForm3.OnReceiveNotificationEvent(Sender: TObject; const ServiceNotification: TPushServiceNotification);
begin
MemoLog.Lines.Add(‘—————————————–‘);
MemoLog.Lines.Add(‘DataKey = ‘ + ServiceNotification.DataKey);
MemoLog.Lines.Add(‘Json = ‘ + ServiceNotification.Json.ToString);
MemoLog.Lines.Add(‘DataObject = ‘ + ServiceNotification.DataObject.ToString);
MemoLog.Lines.Add(‘—————————————‘);
end;

procedure TForm3.FormCreate(Sender: TObject);
var
PushService: TPushService;
ServiceConnection: TPushServiceConnection;
Notifications: TArray<TPushServiceNotification>;
begin
PushService := TPushServiceManager.Instance.GetServiceByName(TPushService.TServiceNames.GCM);
MemoLog.Lines.Add(‘#1 done’);

ServiceConnection := TPushServiceConnection.Create(PushService);
MemoLog.Lines.Add(‘#2 done’);

ServiceConnection.Active := True;
MemoLog.Lines.Add(‘#3 done’);

ServiceConnection.OnChange := OnServiceConnectionChange;
MemoLog.Lines.Add(‘#4 done’);

ServiceConnection.OnReceiveNotification := OnReceiveNotificationEvent;
MemoLog.Lines.Add(‘#5 done’);

FDeviceId := PushService.DeviceIDValue[TPushService.TDeviceIDNames.DeviceId];
MemoLog.Lines.Add(‘#6 done’);

MemoLog.Lines.Add(‘DeviceID: ‘ + FDeviceId);
MemoLog.Lines.Add(‘Ready to receive!’);

// Checks notification on startup, if application was launched fromcold start
// by tapping on Notification in Notification Center
Notifications := PushService.StartupNotifications;
if Length(Notifications) > 0 then
begin
MemoLog.Lines.Add(‘—————————————–‘);
MemoLog.Lines.Add(‘DataKey = ‘ + Notifications[0].DataKey);
MemoLog.Lines.Add(‘Json = ‘ + Notifications[0].Json.ToString);
MemoLog.Lines.Add(‘DataObject = ‘ +
Notifications[0].DataObject.ToString);
MemoLog.Lines.Add(‘—————————————–‘);
end;

end;

end.

Comments are closed.