Search code examples
c#facebookfacebook-requests

Sending notifications / requests to application users (Facebook - C#)


Title may not describe what I actually want, but I hope this is not a duplicate question as I couldn't find a relevant answer in my searches.

I'm developing a Facebook raid manager application for my own World of Warcraft guild and what I want it to do is:

  • When an officer creates a raid, send notifications (or app requests) to all application users whether they are friends of the officer or not.
  • This request can be a user-to-user or app-to-user request, the only important thing is it must trigger a notification on Facebook side.

Again, to be clear, I'm not trying to send application requests to non-application users. I just want a way to send requests to application users triggered by a user event.

Application already has a "Facebook Group" authorization, which means all app users are members of a specific facebook group (not a page).

Is this possible via Facebook dialogs?


Solution

  • Here is some rough code to send an app request using the C# SDK:

    private string getAppAccessToken()
    {
        string facebookAppId = ConfigurationProvider.GetSetting("FacebookAppId");
        string facebookAppSecret = ConfigurationProvider.GetSetting("FacebookAppSecret");
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
            String.Format("https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id={0}&client_secret={1}",
            facebookAppId, facebookAppSecret));
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        var responseStream = new StreamReader(response.GetResponseStream());
        var responseString = responseStream.ReadToEnd();
    
        if (responseString.Contains("access_token="))
        {
            return responseString.Replace("access_token=", string.Empty);
        }
    
        return "";
    }
    
    private void postRequest(string id, string message)
    {
        FacebookClient app = new FacebookClient(this.getAppAccessToken());
        dynamic parameters = new ExpandoObject();
        parameters.message = message;
        parameters.title = "Please use our app";
        dynamic myId = app.Post(id + "/apprequests", parameters);
    }
    

    The notification should appear under App Center / Requests.

    Hope this helps.