Web Niraj
  • Facebook
  • Flickr
  • Github
  • Linkedin
  • Twitter
  • YouTube
Online portfolio, code examples and developer blog
  • About
  • Contact
  • Portfolio
  • WordPress
Search the site...
  • Home
  • Blog
  • Facebook API: App Notifications API for Facebook Canvas Apps

Facebook API: App Notifications API for Facebook Canvas Apps

24

Facebook recently re-introduced app-to-user notifications on the Graph API, allowing developers to notify app users of events easily. This is an alternative to sending the user emails, and doesn’t required any additional permissions. Facebook controls the notifications so developers don’t use it to spam – something that happened with the old API.

If the notification is hidden / marked as spam by users, it will slowly be disabled for your app. Note that the API is currently in beta so it is subject to change. The Notifications API is only available to Canvas / Fan Page applications hosted on apps.facebook.com.

Using the new notifications API is very simple, and only required an app access_token. As long as the user has your app installed, you are free to notify them about in-app events. Here’s how to make the API call:

The $response will be a boolean value indicating success or failure.

The template can be plain-text, like the example above, or use Facebook IDs, like “{60506094} sent you a message“.

The href parameter will always point to your App Canvas Page; you just need to specify a relative path to your page.

The access_token is your app access_token, made up of your application ID, a pipe, and your applications secret:

$app_access_token = $app_id . '|' . $app_secret;
Facebook, Facebook Graph API, Notifications

24 comments on “Facebook API: App Notifications API for Facebook Canvas Apps”

  1. James says:
    January 3, 2013 at 7:24 PM

    Thank you so much for this tutorial, i’ve been searching the internet for aaaages to find this out in simply terms 😀 😀

    Reply
  2. anuj says:
    June 2, 2014 at 6:29 AM

    this is not work

    Reply
    • Niraj Shah says:
      June 6, 2014 at 1:24 PM

      If you want help, you need to provide information on the error message you are seeing, or the code you are using.

      Reply
  3. Diones de Souza says:
    June 10, 2014 at 8:26 AM

    muito obrigada, funcionou corretamente…

    Translated from Portuguese: thank you, it worked correctly …

    Reply
  4. b0b0123 says:
    July 31, 2014 at 4:11 PM

    Is it possible to use this to notify someone who is using your app that someone has tagged them in a photo and then redirect them to an URL?

    Reply
    • Niraj Shah says:
      August 2, 2014 at 1:38 PM

      Yes you can, however notifications can only be used with Canvas Apps, and the link will always go to the canvas URL.

      Reply
      • b0b0123 says:
        August 4, 2014 at 10:53 AM

        This is a really stupid question but… is my canvas URL the apps.facebook.com/myapp? When I load my localhost url and login, it directs me to my canvas URL.. so it is a canvas app right?

        If I have multiple pages/scripts… would that still work with the same canvas URL?

      • Niraj Shah says:
        August 4, 2014 at 3:39 PM

        Yes, that’s the canvas URL. If you have multiple pages, you can access them like you would with a regular website, e.g. https://apps.facebook.com/yourapp/page.php.

      • b0b0123 says:
        August 4, 2014 at 3:36 PM

        Is it possible to send a notification to someone who isn’t using your app? Having done some more research… I’ve realised that I cannot use the id from /taggable_friends to set the privacy – CUSTOM – ALLOW:{id}. Also can’t use this id to send notifications………. And that I can’t get friend id unless they are already using the app. Is this right? Is there any other ways to get friend id?

      • Niraj Shah says:
        August 4, 2014 at 3:41 PM

        No. You can only send notifications to users who use and have authorised your app. The only way to get friend IDs is using the /me/friends end-point, which will only return friends who use your application.

  5. b0b0123 says:
    August 4, 2014 at 4:01 PM

    So the only way to contact someone who isn’t using your app is via email?? And I can’t set the privacy to allow certain people to see post unless they are already using the app? Oh, so confusing!!! And so restricted! But how do people find out about your app if it doesn’t let people send requests/notifications?

    (Sorry, going off topic again – doesn’t relate to your page. Feel free to delete)

    Reply
    • Niraj Shah says:
      August 5, 2014 at 10:10 AM

      Yes, the best way to notify users is to send emails or share things to the timeline publicly (to all users). You can also Invite users to use your app, but this is limited to Games only.

      Reply
      • b0b0123 says:
        August 5, 2014 at 10:25 AM

        Thank you Niraj Shah, really, really appreciate all your help and patient in replying to my stupid questions! 🙂

  6. b0b0123 says:
    August 5, 2014 at 11:01 AM

    Do you think you can do a tutorial on notifications for the new version 2.0?

    Reply
  7. b0b0123 says:
    August 6, 2014 at 11:52 PM

    I am trying to do this with sdk 4.0 and I just realised user access token is different to app access token. Do you know how I can get the app access token? The facebook documentation says you make an api request:

    GET /oauth/access_token?
    client_id={app-id}
    &client_secret={app-secret}
    &grant_type=client_credentials

    But I don’t know how exactly to use this in my php. Can you give me some guidelines?

    Reply
    • Niraj Shah says:
      August 7, 2014 at 12:49 PM

      You can easily get a App Access Token by doing the following using the Facebook PHP SDK 4.0.x:

      $appSession = FacebookSession::newAppSession();

      Reply
      • b0b0123 says:
        August 8, 2014 at 9:54 AM

        Oh right! Would I need to initialise the id and secret etc too along with it? Or could I just used this in the notification parameters for “access_token”? Thanks.

      • Niraj Shah says:
        August 8, 2014 at 10:52 AM

        Yes, anything you do with the SDK needs to be initialised first. The API doesn’t work without the App ID and Secret.

  8. b0b0123 says:
    August 8, 2014 at 9:58 AM

    Oh. I found the page now: https://developers.facebook.com/docs/php/FacebookSession/4.0.0

    Thanks. 🙂

    Reply
  9. b0b0123 says:
    August 8, 2014 at 11:07 AM

    I am getting the error that the URL cannot be found on the server…. the url on it’s own works but not via the notification.

    Thanks for the stackoverflow answer by the way 🙂

    Reply
    • Niraj Shah says:
      August 8, 2014 at 11:11 AM

      Notifications always point to your canvas URL so make sure your URL works from the canvas page. See what URL is in the address when clicking on the notification, it should give you a clue on where its pointing.

      Reply
  10. b0b0123 says:
    August 8, 2014 at 11:15 AM

    It works now! I just put “index.php” instead of the whole url link and it works. Thank you so much though for constantly pointing me in the right directions and helping me out!!!!!!!!!! 😀

    Reply
  11. Stefu Catalin says:
    August 10, 2015 at 5:51 PM

    ERROR! 62An active access token must be used to query information about the current user.

    Reply
    • Niraj Shah says:
      August 14, 2015 at 8:12 PM

      Include a valid access token for the user. i.e. you need to login first.

      Reply

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

StackExchange / StackOverflow

profile for Niraj Shah on Stack Exchange, a network of free, community-driven Q&A sites

Support Me

Buy Me a Coffee

PSN Profile

Tags

ACL Amazon Amazon Web Services Android Android 4.4 KitKat Android 5.0 Lollipop Apache Backup Bug Command Line Cordova cPanel / WHM Facebook Facebook Graph API Facebook PHP SDK 4.0 Facebook Social Plugins Fan Page Firewall Flash Gadget Geolocation Google Nexus 5 Hacking HTML5 iOS JavaScript jQuery Laravel 5 Linux NodeJS Parse PDF PHP Plugin Portfolio PS4 Review Security Server SSH SSL Sysadmin Tutorial WordPress WordPress Plugins
© 2011-2025 Niraj Shah
  • Blog
  • Portfolio
  • WordPress
  • About Me
  • Contact Me
  • Privacy Policy
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Privacy Policy