rAthus
rAthus

Reputation: 894

Facebook PHP API : delete a notification previously sent by my app

I'm sending notifications to my application users like this :

require_once('php-sdk/facebook.php');
$config = array
(
    'appId' => 'blabla',
    'secret' => 'blablablabla',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$post = $facebook->api('/'.$user_id.'/notifications/', 'post',  array
    (
        'access_token' => 'blabla|blablablabla',
        'href' => 'https://www.my_app_url.net/',
        'template' => "You have X new updates waiting on MyAppName !",
        'ref' => 'MyApp_notification'
    ));

Now, if the user have a new update on my app but haven't visited since last notification, I send him a new notification with the new number of updates waiting for him, which could be very annoying if he doesn't connect for a while and has 50 new facebook notifications...

So my question is : Is it possible to remove previous notification(s) on user's Facebook when I send a new one using the PHP API ?

I've searched but couldn't find the answer...

Upvotes: 0

Views: 650

Answers (1)

rAthus
rAthus

Reputation: 894

I have found how to mark notifications as read.

require_once('php-sdk/facebook.php');
$config = array
(
    'appId' => 'blabla',
    'secret' => 'blablablabla',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
if ($user_id) // Only if the user is connected
{
    $permissions = $facebook->api("/me/permissions");
    if (array_key_exists('manage_notifications', $permissions['data'][0])) // Check if we have the permission to manage the notifications
    {
        $access_token = $facebook->getAccessToken();
        $get = $facebook->api('/me/notifications?include_read=0&access_token='.$access_token, 'get'); // We get all the unread notifications from the app
        for ($i=0; $i<count($get['data']); $i++) // Now let's mark each of them as read
        {
            $notification_id = $get['data'][$i]['id'];
            $facebook->api('/'.$notification_id.'?unread=0&access_token='.$access_token, 'post');
            echo 'Mark '.$notification_id.' as read : OK<br>';
        }
    }
    else
    {
        echo 'err0r: user have not allowed the app to manage his notifications';
    }
}
else
{
    echo 'err0r: no user connected to the app';
}

It seems that there is no way to DELETE some notifications, so this is the best solution I have found... hope this will help anyone who have the same problem !

Here's a link to the full code on my PasteBin : http://pastebin.com/2J0c5L18

Upvotes: 1

Related Questions