Last Updated: September 09, 2019
·
1.691K
· safetyscissors

Using NET::HTTP in Rails to send Facebook App Notifications

I've had this problem today where this Facebook graph call has been giving me a little bit of grief in Rails.

So anyway here is the syntax for the Facebook Notification API Call

https://graph.facebook.com/{recipient_userid}/notifications?access_token={application_access_token}&template={Your_Message}&href={URL_to_your_app_or_request}

In your Rails method it should look like this:

require 'koala'
require 'net/http'
require "net/https"
...

fb_url = "#{fb_id}/notifications?access_token=#{app_access_token}&href=#{FB_CALLBACK_URL}&template=You have a new score! See your results now!"
uri = URI.encode("https://graph.facebook.com/#{fb_url}")
uri = URI.parse(uri)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

fb_req = Net::HTTP::Post.new(uri.request_uri)
fb_response = http.request(fb_req)

Some notes:
• I have used the Koala (https://github.com/arsduo/koala) gem to make my life easier.
• Make sure you parse your URI before you encode as to remove any special characters.

Hope this helps anyone out there :)

2 Responses
Add your response

Great, thanks!!

over 1 year ago ·

This is a total newb question but where in the rails app does this go? The model? Routes?

over 1 year ago ·