Saturday, March 3, 2018

Xamarin Android Facebook GameRequestDialog not calling callbacks

Leave a Comment

I'm using the Xamarin.Facebook.Android package for this. The dialog displays correctly and the requests go through to the correct recipients but the callbacks never get called. I'm not 100% sure where I'm supposed to get the CallbackManager from.

public class FacebookService : IFacebookService {     public void InviteFriends(FacebookInviteCallbacks facebookInviteCallbacks)     {         if (AppInviteDialog.CanShow())         {             var activity = Xamarin.Forms.Forms.Context as Activity;             var content = new GameRequestContent.Builder().                 SetTitle("Check out this game").                 SetMessage("Check out this game I play!").                 Build() as GameRequestContent;              var dialog = new GameRequestDialog(activity);              dialog.RegisterCallback(CallbackManagerFactory.Create(), new MyFacebookCallback(facebookInviteCallbacks));             dialog.Show(content);                          }     }        }  public class MyFacebookCallback : Java.Lang.Object, IFacebookCallback {     private FacebookInviteCallbacks facebookInviteCallbacks;      public ImcFacebookCallback(FacebookInviteCallbacks facebookInviteCallbacks)     {         this.facebookInviteCallbacks = facebookInviteCallbacks;     }      public void OnCancel()     {         System.Diagnostics.Debug.WriteLine("Cancelled sending invite");     }      public void OnError(FacebookException error)     {         facebookInviteCallbacks.FailedCallback(error.Message);     }      public void OnSuccess(Java.Lang.Object result)     {         System.Diagnostics.Debug.WriteLine(result);     } } 

Update

From what I can tell from other examples I've seen, CallbackManagerFactory.Create() is correct.

1 Answers

Answers 1

After a lot of digging and trial and error I finally got this working.

In my FacebookService class I added:

public static ICallbackManager callbackManager; 

In the Invitefriends method, in the if statement I changed this:

dialog.RegisterCallback(CallbackManagerFactory.Create(), new MyFacebookCallback(facebookInviteCallbacks)); 

to

callbackManager = CallbackManagerFactory.Create(); dialog.RegisterCallback(callbackManager, new ImcFacebookCallback(facebookInviteCallbacks)); 

In the MainActivity I added the following override.

    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)     {         base.OnActivityResult(requestCode, resultCode, data);                                  Services.FacebookService.callbackManager.OnActivityResult(requestCode, (int)resultCode, data);     } 

That's all it took. I wish the package would have been better documented.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment