androidservice notification does not work

Asked

Viewed 74 times

1

I created a multidevice application and added a local androidservice. I need to issue notifications through the service, follow Unit:

unit AndroidServiceDMUnt;

interface

uses
  System.SysUtils, System.Classes, System.Android.Service, AndroidApi.JNI.GraphicsContentViewText, 
  Androidapi.JNI.Os, Data.DB, MemDS, DBAccess, MyAccess, System.Notification, System.Threading;

type
  TDM = class(TAndroidService)
    MyConn: TMyConnection;
    Qcards: TMyQuery;
    Qcardsimagem: TBlobField;
    NotificationCenter1: TNotificationCenter;
    function AndroidServiceStartCommand(const Sender: TObject; const Intent: JIntent; Flags, StartId: Integer): Integer;
  private
    { Private declarations }
  public
    { Public declarations }
    T:ITask;
    procedure LaunchNotification(name,title,body:string);
  end;

var
  DM: TDM;

implementation

{%CLASSGROUP 'FMX.Controls.TControl'}

{$R *.dfm}
uses Androidapi.JNI.App;

function TDM.AndroidServiceStartCommand(const Sender: TObject; const Intent: JIntent; Flags, StartId: Integer): Integer;
begin
  Result := TJService.JavaClass.START_STICKY;

  T := TTask.Run (procedure
  begin
    While true do
    begin
      sleep(10);
      LaunchNotification('Exemplo','ServicoOnLine','Mensagem de teste!');
      exit;
    end;
  end);

  Qcards.SQL.Text := 'UPDATE cards SET c_tipo = 1000 WHERE c_id = 1';
Qcards.ExecSQL;
end;

procedure TDM.LaunchNotification(name,title,body:string);
var
  MyNotification: TNotification;
begin
  myNotification := NotificationCenter1.CreateNotification;
  try
    MyNotification.Name := 'ServiceNotification';
    MyNotification.Title := 'Android Service Notification';
    MyNotification.Number := 1;
    MyNotification.AlertBody := 'hello host, Im your service';
    MyNotification.FireDate := Now;
    NotificationCenter1.PresentNotification(MyNotification);
  finally
    myNotification.DisposeOf;
  end;
end;

end.
  • Query works but notification does not appear.

Main application Unit code:

unit frmAplicacaoUnt;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, System.Android.Service,
  FMX.Controls.Presentation, FMX.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    ConexaoServico : TLocalServiceConnection;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.Button1Click(Sender: TObject);
begin
  ConexaoServico := TLocalServiceConnection.Create;
  ConexaoServico.StartService('ServicoPost');
end;

end.

Need any additional configuration or code to trigger service notification? I am using Delphi 10.3.2 Rio.

  • Reopen the question please enter more details.

  • Two votes left to reopen.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.