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;
Thank you so much for this tutorial, i’ve been searching the internet for aaaages to find this out in simply terms 😀 😀
this is not work
If you want help, you need to provide information on the error message you are seeing, or the code you are using.
muito obrigada, funcionou corretamente…
Translated from Portuguese: thank you, it worked correctly …
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?
Yes you can, however notifications can only be used with Canvas Apps, and the link will always go to the canvas URL.
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?
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
.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?
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.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)
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.
Thank you Niraj Shah, really, really appreciate all your help and patient in replying to my stupid questions! 🙂
Do you think you can do a tutorial on notifications for the new version 2.0?
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?
You can easily get a App Access Token by doing the following using the Facebook PHP SDK 4.0.x:
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.
Yes, anything you do with the SDK needs to be initialised first. The API doesn’t work without the App ID and Secret.
Oh. I found the page now: https://developers.facebook.com/docs/php/FacebookSession/4.0.0
Thanks. 🙂
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 🙂
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.
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!!!!!!!!!! 😀
ERROR! 62An active access token must be used to query information about the current user.
Include a valid access token for the user. i.e. you need to login first.