Facebook Cookbook - Jay Goldman [152]
Figure 9-15. PackRat Notification received
Figure 9-16. PackRat Notification sent
As described in Understanding Allocations, your application has an allocated number of Notifications that you can send per day, based on how users have received your Notifications in the past. Since users who mark your Notifications as spam today affect the number of Notifications you can send tomorrow, it’s critical to your app’s success that you spend time on a Notification strategy that will keep your so-called “spamminess” rating as low as possible. There’s no one strategy that will work for all apps, but here’s some general advice.
Sending to friends
You can send Notifications only to the current loggedinuser or to her friends who have your app installed. This works well for games such as PackRat, since you can only play with your friends anyway. It works much less well in a game in which you can challenge players who aren’t your friends, since there’s no way to send them a notification when it’s their turn. You can get around this by creating infinite sessions for them and storing their session_keys in your database, and then making an API call on their behalf to send them a Notification, though this can become quite tedious. See Creating an Infinite Session Key for more information.
Monitoring allocations
Here are two ways you can monitor your allocation of Notifications:
Check the Allocations tab in the Facebook Insights application for your app (http://www.facebook.com/business/insights/app.php?id=123456789&tab=allocations, where 12345679 is your app’s ID).
Use the Admin.getAllocation() method. See Getting Allocations for more info.
You should generally keep an eye on those numbers, but you should be particularly watchful when you make a change to the Notifications sent by your app, as that’s most likely to trigger a change in the way recipients are receiving them.
Get Notifications
Problem
I need to retrieve all of the outstanding Notifications for the current user.
Solution
Use the Notifications.get() method:
$notifications = $facebook->api_client->notifications_get();
Discussion
Notifications.get() returns a multidimensional array in which the elements are different Notification types (messages, pokes, shares, friend requests, group invites, and event invites), with each one containing different information depending on its type. Messages, pokes, and shares all contain an unread count and the most_recent ID of the relevant content type, friend requests contains the uids of the users who have made the outstanding requests, and group and event invites contain the gids and eids of the relevant groups and events.
Facebook encourages developers who are building apps that notify users of new messages, pokes, and shares to use the following logic:
if (unread > 0 && most_recent > old_most_recent) {
display_notification();
}
old_most_recent = most_recent;
Get Pages
Problem
I need to find all of the Pages that the current user is a fan of.
Solution
Use the Pages.getInfo() method:
$fields = array('name','pic_small','has_added_app');
$pages = $facebook->api_client->pages_getInfo(null,$fields,null,null);
Discussion
The four parameters are filters that allow you to constrain the result set. The first is for pageids (Page IDs), which allows you to retrieve info for a specific Page or a set of Pages:
$pages = array('25975037248', '5603889283');
$fields = array('name','pic_small','has_added_app');
$pages = $facebook->api_client->pages_getInfo($pages,$fields,null,null);
The second parameter is the set of fields you’d like returned in your results, which can include any of the fields listed in Table 8-15. The third is for uids and allows you to return the Pages for a different user (or set of users), provided that the current loggedinuser is allowed to see that info:
$users