I'm trying to create a setup via PHP where I can access if I have any recent Facebook notifications via Facebook SDK. I want to be able to check recent friend requests, messages and general notifications.
I have became a developer and have downloaded and initialized the Facebook SDK. From this I can access Graph API where I can acquire information about the user. From this I can conduct checks every so often about user information. I have conducted extensive research but finding out if I have any recent notifications via API graph I can't find?
I have also read about real time updates where a call back URL would be triggered when a piece of data has changed, this could be Facebook notifications. This would be more efficient however I'm having trouble working with this.
I understand these concepts and have researched however I can't find out how to use AIP graph and real time updates to check for recent notifications and report back.
Any Facebook developers can help?
After poking around enough with the good old research and development I found a versatile way of checking my recent Facebook notifications via Facebook API graph.
The initial question had versatility in mind in terms of a check could be conducted by simple HTTP requests without the need for accruing anything like session cookies. This allowed me to use CURL requests in a remote php script that was accessed by a small Arduino server to get the information. The Arduino server could then turn on LED's if new notifications were present.
I created an app called Notifier becoming a developer with Facebook and this allowed me to create something called an Access Token for users or me. This was a long string and was valid for 60 days and allowed me access my Facebook account information. I acquired it through implementing the Facebook SDK in a small piece of code with the permissions that allowed to access information regarding my notifications.
After implementing the Facebook SDK in php with my app and after signing in through me I could call $facebook->getAccessToken();
to acquire it. This can be then saved in database or hard coded for development purposes. Initially it only lasts for 60 days however extending its life is possible. An alternative is just getting the user or me to sign into a web application every so often.
A debug access token that lasts an hour can be obtained within the Facebook developer graph explorer tool. These permissions were as follows :
'read_mailbox, manage_notifications, read_requests'
After accruing an access token three different HTTP requests to the API Graph which is Facebook's database of information can be requested to get the necessary information to check if a user does have recent notifications.
Friends requests could be accessed via the following HTTP request to the API graph :
https://graph.facebook.com/me/friendrequests?limit=0&access_token=$accessToken
$AccessToken
being a valid access token with the permissions as mentioned above.
This would respond with a JSON data response which can be processed to find a unread_count
.
The is the same for messages and general notifications.
Request of unread_count
of messages :
https://graph.facebook.com/me/inbox?limit=0&access_token=$accessToken
Request of unseen_count
of general notifications :
https://graph.facebook.com/me/notifications?limit=0&access_token=$accessToken
'limit=0' within the requests means you don't actually want to request the notification data or objects. Which is true. All JSON responses respond with a 'summary' object which includes a 'unseen_count' or 'unread_count'. This is the data that we want.
An example of a JSON response from the message HTTP request is as follows :
{
"data": [
],
"summary": {
"unseen_count": 0,
"unread_count": 16,
"updated_time": "2014-04-08T18:30:48+0000"
}
}
Within PHP json_decode
can be used to process the JSON response and accrue the raw 'unread_count'.
Most users have an existing unread_count from acquired messages which means to test if the user has any recent notifications present a change must be tested. This can be done by reading a value then reading the same value moments later and then compared. If the second reading has increased then the user has a new message notification!