Facebook messenger detect closing webview (mobile)
Asked Answered
S

3

10

I have a messenger bot that provides users with links to a webpage. Is there a way to detect when the user closed the webview and therefore returned to bot conversation?

This question only applies to Messenger app on mobiles phones, desktop is fine.

window.onbeforeunload isn't supported, window.pagehide only works on reload but not on closing the webview as well as window.unload.

Spoor answered 9/3, 2017 at 10:1 Comment(8)
Are you using the Messenger Extensions JS SDK for your webview?Pentane
@SLee yes, but haven't found anything in the docs that would helpSpoor
Did you solve your issue?Poikilothermic
@Marco Dufal nope, the issue is still openSpoor
@Spoor do you have any news ? I m facing same challengeArriaga
How did you do to manage it on desktop ?Arriaga
@MohamedELAYADI sadly no update for you. I haven't found a way (after some time I just gave up). As for desktop, it behaved differently a year ago and it was not an issue. Now with their iframe, I am not sure how this will work.Spoor
Thanks a lot @Elwhis; I ended up setting websocket communication... there is no way to do so... Facebook removes the whole iframe ... and there is no way to prevent it nor detecting itArriaga
V
0

You could try using this.

MessengerExtensions.requestCloseBrowser(function success() {
  // webview closed
}, function error(err) {
  // an error occurred
});

It's not the same as what you are looking for. But you could drive the users this way if you are implementing the webview website also.

For more information: Messenger Extension Webview

Versieversification answered 30/9, 2018 at 7:10 Comment(0)
N
0

This actually depends on what you want to achieve in your flow.

If the user closes the webview using the close-x (upper right) you should think of this as regular close event and don't handle anything at all. Instead you should use a dedicated save/confirm and/or close button in your webview.

The close button then triggers the requestCloseBrowser function of the Messenger Extension SDK and the save/confirm button handles the actual save/confirm/process action.

If you really need to detect a user closing the webview by using the close-x in the upper right this can only be achieved by long-polling (polling a user session from you webview).

Nixie answered 25/6, 2019 at 14:43 Comment(0)
M
-2

It's quite simple we need to use Facebook's messenger extension SDK, Last year i implemented this. you can find a relevant code here,

https://github.com/vickymicky/botkit-messenger-express-demo/commit/4aa91d12167daa322bdaddb158289b74ee9610c7

We need to use window.extAsyncInit to detect the event and then we can pass on the info to the server.

Use this MessengerExtensions.getUserID to get user id if required.

Refer https://github.com/vickymicky/botkit-messenger-express-demo/blob/master/views/webview.ejs

Note: Above example is done on Node.js.

Mccombs answered 19/4, 2017 at 9:37 Comment(4)
It's not an answer to the question. You are forcing the webview to close, but I want to detect the case when user closes the webview by himself.Spoor
True, it's also described in the official documentation here: developers.facebook.com/docs/messenger-platform/webview/closeSheya
@Spoor #45924427Sheya
#43280278Sheya

© 2022 - 2024 — McMap. All rights reserved.