5

I'm trying to execute callback function in FB.ui (send dialog). It called in same moment when loaded FB.ui but I want execute callback function after pressing 'send' or 'cancel' button. Is it realizable?

function callback(response) {
      alert('message was sent');
}

FB.ui({
                method: 'send',
                name: 'Dialog',
                link: 'http://***.com',
                redirect_uri: '****',
                description: '***',
                picture: '***',
                to: userId
            },
                callback(response)
            );
Al Puz
  • 53
  • 1
  • 4

4 Answers4

3

The callback requires a function as a parameter. Here, you are actually calling the function.

Facebook will actually call the function that you pass it along with the response.

Rather than passing "callback(response)" as the callback parameter, just pass "callback" like this:

function callback(response) {
      alert('message was sent');
}

FB.ui({
       method: 'send',
       name: 'Dialog',
       link: 'http://***.com',
       redirect_uri: '****',
       description: '***',
       picture: '***',
       to: userId
       },
       callback
);
Fisch
  • 3,775
  • 1
  • 27
  • 38
  • 3
    The above is indeed correct use of the callback parameter, but requires a bit more commentary. 1) Documentation states "not all methods may have a response", which is partially true here, the callback is called whether the send dialog is sent or canceled, but none of the user input is passed to the callback. 2) My tests shows that when Send is clicked, [] (empty array) is passed to the callback, when canceled, undefined is passed, so I use the following code to branch my logic based on the user's action: `if (!!response) { /* sent clicked */ } else { /* cancel clicked */ }` – Mike Jarema Jun 23 '12 at 02:45
  • I wonder how reliable that 'Sent = [] (empty array), Canceled = undefined' behavior is.... – Tyguy7 Sep 17 '14 at 21:35
  • 2
    @Tyguy7 Looks like it is not reliable... As it stands, hitting "cancel" and "send" both result in [ ], while hitting 'X' results in 'undefined.' – D'Arcy Rail-Ip Nov 05 '15 at 21:35
2

Issue has probably been solved or is irrelevant to OP, but this might help some others out that stumble upon the post.

Similar to Mike Jerema's comment on Fisch's reply (Unable to comment there due to insufficient rep):

The response callback receive from the user interacting with the send dialog returns one of three things:

  1. If Send is clicked an empty array is passed back (instanceof Object)
  2. If Cancel is clicked null is returned
  3. If the 'X' in the corner of the dialog is clicked, undefined is returned.

Therefore the correct code to handle this callback for all use cases would be:

var callback = function(response) {
               if (response instanceof Object)
               {        
                   //Send clicked

                }
                else if (response === null)
                {
                    //Cancel clicked
                }
                else 
                    //X clicked
            };

Hope this helps! :)

Lionz
  • 21
  • 2
  • As of now, this does not seem correct any more. I just tried to check if the user clicked cancel or shared something, but in both cases, an empty array is returned. Very confusing! – ConcurrentHashMap Sep 29 '14 at 15:48
  • Since this is not official, if someone implements this, they constantly need to keep checking if the functionality continues to work. FB can change it or even remove it anytime. Useful while it exists, otherwise probably timers... – Kazim Zaidi Jun 30 '17 at 08:27
1

Yo, I'm not sure if it's your case, but it seems there's a bug when testing on development environments, mostly due an issue related to the port parameter in a url.

Check this out: http://developers.facebook.com/bugs/380566711996797

wasted like 2 hours freaking out due to this "bug"

Sikora
  • 766
  • 6
  • 6
0

This was driving me nuts for over 2 hours on the feed dialog, then as a last ditch I added the display property

method: 'feed',
display: 'popup'

This line isn't necessary as it is default, but maybe it forces the dialog to behave correctly, and it got me out of a hole. Worth a try on the send dialog?