Jump to content
John van de Waeter

How to know how app was started

Recommended Posts

Hi All,

 

The phone (iOS or Android) receives a push notification.

The user taps the notification and the app is started or brought to front.

 

It's nice behaviour if the app responds according to the notification.

 

How to know if the app was started / brought to front via a tap on the notification?

 

cheers,

John

Share this post


Link to post

The notification is (at least when I last checked) "received" before the app becomes active, so you could have a flag that is set when the BecameActive event fires (to indicate the app was launched), and another to indicate whether the app went into the background, and set that when the EnteredBackground event fires, e.g.:

 

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, System.Messaging,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs;

type
  TForm1 = class(TForm)
  private
    FIsLaunched: Boolean;
    FIsBackground: Boolean;
    procedure ApplicationEventMessageHandler(const Sender: TObject; const M: TMessage);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

uses
  FMX.Platform;

{ TForm1 }

constructor TForm1.Create(AOwner: TComponent);
begin
  inherited;
  TMessageManager.DefaultManager.SubscribeToMessage(TApplicationEventMessage, ApplicationEventMessageHandler);
  FIsLaunched := False;
  FIsBackground := True;
end;

destructor TForm1.Destroy;
begin
  TMessageManager.DefaultManager.Unsubscribe(TApplicationEventMessage, ApplicationEventMessageHandler);
  inherited;
end;

procedure TForm1.ApplicationEventMessageHandler(const Sender: TObject; const M: TMessage);
begin
  case TApplicationEventMessage(M).Value.Event of
    TApplicationEvent.BecameActive:
    begin
      FIsLaunched := True;
      FIsBackground := False;
    end;
    TApplicationEvent.EnteredBackground:
      FIsBackground := True;
  end;
end;

end.

When the app receives a notification: if the app was being launched from not running, FIsLaunched should be False (does not matter what FIsBackground is), if launched from the background, FIsLaunched and FIsBackground should be True.

Share this post


Link to post
On 4/20/2022 at 1:10 AM, John van de Waeter said:

How to know if the app was started / brought to front via a tap on the notification?

On Android, there is a property in FMX (I can't find it right now) that contains the Intent that started the app if it was not already running.  You can check that Intent at startup to see if your app was started because of a notification, a file open, etc, depending on how your app manifest is configured.  If your app was already running when the Intent is sent, you can register with FMX's RTL to receive TMessageReceivedNotification messages, which will carry the Intent.

 

I'm not sure of the equivalent for iOS, though I do notice that TPushRemoteNotificationMessage and TPushStartupNotificationMessage exist for it.

         

 

Edited by Remy Lebeau
  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×