Invite Friends API

One of the most common features of the social games which are built on the Facebook platform, is the ability for players to invite their friends to play the game with them.

Unfortunately, the Invitable Friends API is only available for apps classified as Games, which also have a Canvas presence. This API is not available in v1.0. It may be called by mobile games as long as they also have a Canvas presence.

It means that it is ONLY possible for games, if your App is NOT a game which you can forget about. And you MUST have a Canvas implementation next to your mobile one. This means that you can´t just create a Canvas App that redirects (which would not be allowed), you will have to create a game that actually runs on Canvas too.

The detailed information about Canvas Apps can be found in the docs. Thx, to luschn for making  it clear via StackOverflow post.

As an event, we can still use AppRequest, which can be used for sending the invites by SendInvite method of SPFacebook class 

public void SendInvite(string title, string message, string data = "", string[] to = null)

 

The code snippet bellow shows how to send an invite and allow user to select the recipients via Facebook default dialog box.

string title = "Hello";
string message = "Play with me";

SPFacebook.instance.SendInvite(title, message);
SPFacebook.instance.OnAppRequestCompleteAction += OnAppRequestCompleteAction;


void OnAppRequestCompleteAction (FBAppRequestResult result) {

	if(result.IsSucceeded) {
		Debug.Log("App request succeeded");
		Debug.Log("ReuqetsId: " + result.ReuqestId);
		foreach(string UserId in result.Recipients) {
			Debug.Log(UserId);
		}

		Debug.Log("Original Facebook Responce: " + result.Result.Text);
	} else {
		Debug.Log("App request has failed");
	}
	SPFacebook.instance.OnAppRequestCompleteAction -= OnAppRequestCompleteAction;
}

 

 

We may also provide more info as specified recipients (in this case friends selector dialog will not be showed) or add the additional request data.

string FriendId = "1405568046403868";
string title = "Hello";
string message = "Play with me";

//Additional freeform data you may pass for tracking. This will be stored as part of the request objects created. The maximum length is 255 characters. (Optional) 
string requestDate = "some_request_data";

SPFacebook.instance.SendInvite(title, message, requestDate, new string[]{FriendId});
SPFacebook.instance.OnAppRequestCompleteAction += OnAppRequestCompleteAction;


void OnAppRequestCompleteAction (FBAppRequestResult result) {

	if(result.IsSucceeded) {
		Debug.Log("App request succeeded");
		Debug.Log("ReuqetsId: " + result.ReuqestId);
		foreach(string UserId in result.Recipients) {
			Debug.Log(UserId);
		}

		Debug.Log("Original Facebook Responce: " + result.Result.Text);
	} else {
		Debug.Log("App request has failed");
	}
	SPFacebook.instance.OnAppRequestCompleteAction -= OnAppRequestCompleteAction;
}

 

Handling request's is similar with the another request type. But since we cannot use the full Facebook invite API FBAppRequestActionType  in the FBAppRequest will remain Undefined. Requests handeling are described in following chapters:

You can read more about the Facebook App requests at the Facebook Official Documentation. The implementation example can be found at the FacebookTurnBasedAndGifts example scene.