2

Is there a way to batch send App-to-User notifications?
Currently I send them one by one using a POST request to the /{recipient_userid}/notifications edge using a php script, and it feels like I'm doing something wrong.

bks
  • 1,886
  • 1
  • 24
  • 43

1 Answers1

1

tl;dr

$userIds = ['100000000000000','100000000000001', '100000000000002']; // Users who will receive the notification.
$notificationMessage = 'Test message!'; // Notification message. Character limit: 180, truncate after 120.
$linkHref = 'posts/123'; // Path after Facebook Web Games URL that facebook's iframe will display on notification click.
$analyticsRef = 'new-posts'; // Group notifications for App Analytics.

var_dump(sendBatchRequests($userIds, $notificationMessage, $linkHref, $analyticsRef));

function sendBatchRequests($ids, $message, $href = '', $ref = null) {
    $APP_ACCESS_TOKEN = '123456789012345|AbCdEfGhIjKlMnOpQrStUvWxYz'; // How to create one: https://stackoverflow.com/a/48831913/1494454
    $FACEBOOK_BATCH_ELEMENT_LIMIT = 50; // Maximum amount of request grouped into a batch. Facebook's limit is 50: https://developers.facebook.com/docs/graph-api/making-multiple-requests#limits

    $results = [];
    $idChunks = array_chunk($ids, $FACEBOOK_BATCH_ELEMENT_LIMIT);
    foreach ($idChunks as $idChunk) {
        $batch = [];
        foreach ($idChunk as $id) {
            $batch[] = [
               'method' => 'POST',
               'relative_url' => "{$id}/notifications?template={$message}" . ($ref ? "&ref={$ref}" : ''),
            ];
        }
        $results[] = file_get_contents('https://graph.facebook.com/', false, stream_context_create([
            'http' => [
                'method'  => 'POST',
                'header'  => "Content-type: application/json; charset=UTF-8",
                'content' => json_encode([
                    'access_token' => $APP_ACCESS_TOKEN,
                    'batch' => $batch,
                ]),
            ],
        ]));
    }
    return $results;
}

Explanation

There is an example on GitHub on how to do this, but it is really old, so I created a modern, up-to-date one inspired by it. Both the notification API and the guide to do batch requests are well documented, so you just need to implement it which can be done in vanilla PHP.

The sendBatchRequests function will send a notification with the $message text for every user given in $userIds. It will group every $FACEBOOK_BATCH_ELEMENT_LIMIT amount of requests into batches and fires one HTTP request per batch. The API response is stored in $results. Clicking the notification will redirect the user to your app's Facebook Web Games URL inside the Facebook Games iframe. You can specify the path after that URL with $linkHref. $ref is for analytic purposes. See more in the documentation linked above.

This is just an example. You still have to make sure that there will be a request for every user and the script doesn't time out and check whether the request was successful.

totymedli
  • 29,531
  • 22
  • 131
  • 165