Telegram-Mini-Apps / issues

Centralised storage of platform issues and enhancement proposals.
11 stars 1 forks source link

Fire an event on app close #26

Open epiphone opened 4 months ago

epiphone commented 4 months ago

Is your feature request related to a problem? Please describe.

I'm trying to call navigator.sendBeacon when the app is closed or hidden. Listening to the visibilitychange and pagehide events works well to detect app hiding but these events are not fired when closing the mini app with the top-left close button (cross icon on Android).

Describe the solution you'd like

One of the following would be great:

  1. Fire the document visibilitychange event before app is closed with the top-left close button, or
  2. expose a callback like Telegram.WebApp.CloseButton.onClick that is called on close button click.

Describe alternatives you've considered

I tried hijacking the back button as follows:

window.Telegram.WebApp.BackButton.show().onClick(() => {
  navigator.sendBeacon(...);

  window.Telegram.WebApp.close();
});

This works by replacing the close button with a back button. However, now when I want to minimize the mini app by swiping left, the app closes instead of minimizing.

Additional context

Tested with the Android Telegram app.