DelphiWorlds / Kastri

Cross-platform library for Delphi
MIT License
498 stars 119 forks source link

[FCMRebooted] Activity closes a few seconds after launching from AndroidIntentServiceHandleIntent #171

Closed AlexUsovDeveloper closed 1 year ago

AlexUsovDeveloper commented 1 year ago

Previously (in the FirebaseCloudMessaging demo) it was possible to launch the application when a push notification was received with the launchapp parameter.

DWFirebaseMessagingService.java:

if (launchapp.equals("1")) {
    // Log.v(TAG, "Launching..");
    this.startActivity(this.getPackageManager().getLaunchIntentForPackage(this.getPackageName()));
}

I tried to do in FCMRelayDemo by analogy:

procedure TServiceModule.AndroidIntentServiceHandleIntent(const Sender: TObject; const AnIntent: JIntent);
var
    LExtras: JBundle;
    LNotification: TAndroidPushServiceNotification;
    LLaunchApp: String;
begin
    TOSLog.d('+TServiceModule.AndroidIntentServiceHandleIntent');
    ...
    LLaunchApp := '0';
    LNotification.JSON.TryGetValue<String>('launchapp', LLaunchApp);
    TOSLog.d('launchapp=' + LLaunchApp);
    if LLaunchApp = '1' then
        if not TAndroidHelperEx.IsActivityForeground then
          TAndroidHelper.Context.startActivity
            (TAndroidHelper.Context.getPackageManager.getLaunchIntentForPackage
            (TAndroidHelper.Context.getPackageName));
    ...
    TOSLog.d('-TServiceModule.AndroidIntentServiceHandleIntent');
end;

But the activity closes a few seconds after launch.

Error messages Logcat:

17:14:53.697  I  id=100801 Removed Bounds for - my.package.name/com.embarcadero.firemonkey.FMXNativeActivity@0#100801 (116)
17:14:53.697  I  id=100802 Removed SurfaceView[my.package.name/com.embarcadero.firemonkey.FMXNativeActivity]@0#100802 (116)
17:14:53.697  I  id=100803 Removed SurfaceView[my.package.name/com.embarcadero.firemonkey.FMXNativeActivity]@0(BLAST)#100803 (116)
17:14:53.697  I  id=100804 Removed Background for SurfaceView[my.package.name/com.embarcadero.firemonkey.FMXNativeActivity]@0#100804 (116)
17:14:53.698  I  Process my.package.name (pid 30369) has died: fg  TOP (253,831)
17:14:53.711  I  WIN DEATH: Window{6c2cab0 u0 my.package.name/com.embarcadero.firemonkey.FMXNativeActivity}
17:14:53.712  W  Input channel object '6c2cab0 my.package.name/com.embarcadero.firemonkey.FMXNativeActivity (client)' was disposed without first being removed with the input manager!
17:14:53.713  V  Remove Window{6c2cab0 u0 my.package.name/com.embarcadero.firemonkey.FMXNativeActivity}:
...

(full logcat output in attached archive)

IDE, device, OS Delphi 11 Update 3 Samsung SM-A525F Android 13, API 33

Steps To Reproduce

  1. Mark permissions "Reorder tasks", "Wake lock" and "System alert window" in project options;
  2. Get ACTION_MANAGE_OVERLAY_PERMISSION permission:
``` procedure TfrmMain.Button1Click(Sender: TObject); var Intent: JIntent; begin if not TJSettings.JavaClass.canDrawOverlays(TAndroidHelper.Context) then begin TMessageManager.DefaultManager.SubscribeToMessage (TMessageResultNotification, HandleActivityMessage); Intent := TJIntent.Create; Intent.setAction(TJSettings.JavaClass.ACTION_MANAGE_OVERLAY_PERMISSION); Intent.setData(TJnet_Uri.JavaClass.parse (StringToJString('package:my.package.name'))); TAndroidHelper.Activity.startActivityForResult(Intent, 5469); end else MessagesMemo.Lines.Add('Permission was granted!'); end; procedure TfrmMain.HandleActivityMessage(const Sender: TObject; const M: TMessage); begin if M is TMessageResultNotification then OnActivityResult(TMessageResultNotification(M).RequestCode, TMessageResultNotification(M).ResultCode, TMessageResultNotification(M).Value); end; function TfrmMain.OnActivityResult(RequestCode, ResultCode: Integer; Data: JIntent): Boolean; begin if (RequestCode = 5469) then if TJSettings.JavaClass.canDrawOverlays(TAndroidHelper.Context) then begin MessagesMemo.Lines.Add('Permission granted'); // end else begin MessagesMemo.Lines.Add('Permission not granted'); // end; Result := True; end; ```
  1. Call startActivity in AndroidIntentServiceHandleIntent:
    TAndroidHelper.Context.startActivity(TAndroidHelper.Context.getPackageManager.getLaunchIntentForPackage(TAndroidHelper.Context.getPackageName));
  2. Send a push notification from the server or through the FCM console.

How to make the activity not close after launch?

log_and_units.zip

DelphiWorlds commented 1 year ago

I have been looking into this. Currently my problem is that the service does not even start. More soon...

DelphiWorlds commented 1 year ago

This issue was solved when this one was.

DelphiWorlds commented 1 year ago

Closing due to lack of feedback