简体   繁体   中英

Facebook webview fails when shared

FINAL EDIT This was a bug. After filing the report it was addressed.

EDIT

I am trying to get user id in a webview of my facebook messenger bot. It works fine on mobile, yet fails on desktop (web). This should not be the case.

Messenger 2.1 release statement gives the following quote:

Desktop support for Extensions SDK: This new feature will extend functionality across mobile and web, creating a consistent experience across devices. Now, features like user ID and sharing that used to only be accessible on mobile will be available on desktop as well. This also provides developers with an easier way to test and debug when implementing webview and chat extensions.

There are two ways to get the user id with messenger extensions: getUserId() and getContext() . The docs state that getUserId() is not available on desktop, but make no mention of getContext() .

Howevew, there is a bug report that states that getContext() call is not yet available on desktop.

The docs mention no other ways of getting user id. How is one supposed to do that?

As a kicker, if you read the original question, you will see that getContext() actually does work on desktop (web), but only if the webview is opened through a link sent directly by the bot.

ORIGINAL

I am working on a Facebook (messenger) bot using webview. Now the most basic of all tasks is to get the userId.

This is where I hit a big problem. Having investigated it thoroughly, below I present cases and results.

  • case 1: my app sends me a generic template with a web_url button (that opens the webview) and a share button.

    Everything works great. I get the user id.

  • case 2: I click share from the message in case 1 , and share it with myself and my app.

    from either message thread (I to myself, or the I to app) getContext() call return error 2018166 Permission not valid to call the SDK API. and askPermission() returns 2018154 Messenger Extensions unexpected error.

  • case 3: I click share from inside the webview using beginShareFlow() and share with myself and the bot.

    same as case 2

  • case 4: here's the kicker.

    activating the webview from case 2 or case 3 from Android, works and gives me the user id.

  • case 5: sharing to a friend who has interacted with the app before.

    When that friend activates the webview, from desktop it fails, from android works.

  • case 6: sharing to a friend who has not interacted with the bot.

    on desktop doesn't work (sharing by button or through beginShareFlow() ), it works!

So, after writing all of this out, the pattern is:

  • on android, webview sharing works as expected: I can get the user id whether they have interacted with the bot befor or not.
  • on desktop, the only time the webview provides the user id, is when the message with the webview link was sent by the bot. Once it is shared by a user, the webview still opens, but does not provide context.

Just to clarify. I get the user id by using getContext , and not by getUserId which the docs specifically say doesn't work on desktop.

Is there anything I can do about this? I would like my bot's webviews to work both on desktop and mobile.

I can think of a workaround, but it's far from ideal

EDIT 2

As requested, the payload for sharing within webview is:

{
  "attachment":{
    "type":"template",
    "payload":{
      "template_type":"generic",
      "elements": [{
        "title":"Testing webview",
        "default_action":{
          "type":"web_url",
          "webview_height_ratio": "full",
          "messenger_extensions": true,
          "url":"https://plenty.life/webview"
        },
        "buttons":[{
          "type":"web_url",
          "webview_height_ratio": "full",
          "messenger_extensions": true,
          "url":"https://plenty.life/webview",
          "title":"Test"
        }]
      }]
    }
  }
}

Try this...

<script>
window.extAsyncInit = function() {
    // the Messenger Extensions JS SDK is done loading 
    console.log("using getUserId in",window.name)

    MessengerExtensions.getUserID(function success(uids) {
    // User ID was successfully obtained. 
        var psid = uids.psid;
        console.log(psid);
        document.getElementById("userId").innerText="your User Id Is "+psid;
    }, function error(err, errorMessage) {      
    // Error handling code
    console.log("some Error",err,errorMessage)
    }); 
    MessengerExtensions.getContext('<appID>', 
        function success(result){
            console.log("success",result)
        },
        function error(result){
            console.log("error",result)
        }
    );
};
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM