O'Reilly logo

Facebook Cookbook by Jay Goldman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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;

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required