Facebook Unity and handling app requests

2019-09-10 03:23发布

问题:

I pulled my original question because I managed to figured it out through trail and error and a lot of deep searching. So, I understand that using the latest Facebook SDK in Unity, you can pull all pending requests for a player using:

FB.API("/me/apprequests", HttpMethod.GET, RequestHandler)

Where RequestHandler is an IGraphResult, which you can then parse into a Dictionary, like so:

void RequestHandler(IGraphResult result){
    if (result != null) {
        Dictionary<string, object> reqResult = Json.Deserialize(result.RawResult) as Dictionary<string, object>;
    }

}

The documentation explains how a singular request will be displayed in JSON format, and I've found a few examples of how to work with that information (I vaguely understand JSON's), however if pulling ALL requests for the player, how do I work with this information?

Out of the JSON, I'm just trying to pull the object ID and sender ID of each request, handle the request based on the object ID then delete the request from the graph by concatenating the two, which I think I've figured out already.

So my question is, for each request, how do I extract the object and sender ID's?

回答1:

So after a LOT of trial and error and a lot of Log checks, I've figured out a really hacky way of doing it, for those that aren't sure:

public void TestRequests(){
    FB.API("/me/apprequests", HttpMethod.GET, TestResponse);
}

public void TestResponse(IGraphResult result){
    if (result.Error == null) {
        //Grab all requests in the form of  a dictionary.
        Dictionary<string, object> reqResult = Json.Deserialize(result.RawResult) as Dictionary<string, object>;
        //Grab 'data' and put it in a list of objects.
        List<object> newObj = reqResult["data"] as List<object>;
        //For every item in newObj is a separate request, so iterate on each of them separately.
        for(int xx = 0; xx < newObj.Count; xx++){
            Dictionary<string, object> reqConvert = newObj[0] as Dictionary<string, object>;
            Dictionary<string, object> fromString = reqConvert["from"] as Dictionary<string, object>;
            Dictionary<string, object> toString = reqConvert["to"] as Dictionary<string, object>;
            string fromName = fromString["name"] as string;
            string fromID = fromString["id"] as string;
            string obID = reqConvert["id"] as string;
            string message = reqConvert["message"] as string;
            string toName = toString["name"] as string;
            string toID = toString["id"] as string;
            Debug.Log ("Object ID: " + obID);
            Debug.Log ("Sender message: " + message);
            Debug.Log ("Sender name: " + fromName);
            Debug.Log ("Sender ID: " + fromID);
            Debug.Log ("Recipient name: " + toName);
            Debug.Log ("Recipient ID: " + toID);
        }
    } 
    else {
        Debug.Log ("Something went wrong. " + result.Error);
    }
}

Again, this is my first experience with using JSON, and I'm sure there's a much more efficient way of doing this, but basically after a lot of breaking down and converting, I've managed to extract the object ID, sender name and ID, the message attached and the recipient name and ID. The object ID comes concatenated with the recipient ID, so to operate on the object ID itself, this will need to be removed, however as is it will make it easier to pass the string on to remove the request from the Graph API.

If anyone can suggest to me a more efficient way of doing this, I'd be grateful! There's always more to learn, after all.