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 Facebook Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.