3

I have created messages to notify users that they have been tagged in a picture or that someone commented after them on a thread; pretty much like facebook-style notifications.

How could I detect if a message targeting a user is new to him?

I had two ideas:

  • compare the timestamp of when the message has been created and the session timestamp. The session timestamp is refreshed every time a session requests a new page so I don't know if I can catch the previous value of this timestamp?
  • catch the same event that triggers drupal_set_message().

Would there be any other method to do that?

A recent post "This is a New Message/You have 4 New Messages" has been submitted in the issue queue of the Message module but apparently, it doesn't contain any built-in function to see if the message is new.

1

I ended up using a custom module with a table containing two columns: uid and lastaccess and two simple functions.

function notifmger_views_post_render(&$view, &$output, &$cache){
    if ($view->name == "notification" && $view->current_display == "page_1"){
        global $user;
        $check = db_query("SELECT * FROM notifmger WHERE uid = :uid",
            array(':uid' => $user->uid))->fetchCol(1);
        if (!empty($check)){
            $update = db_query("UPDATE notifmger SET lastaccess = :time WHERE uid = :uid", array(":time" => time(), ":uid" => $user->uid));
        }
        else {
            $insert = db_insert('notifmger')
            ->fields(array(
              'uid' => $user->uid,
              'lastaccess' => time()
            ))->execute();
        }
    }
}

function notifmger_counter() {
    global $user;
    $msgs = db_query("SELECT count(*) FROM message LEFT JOIN notifmger ON notifmger.uid = message.uid WHERE message.uid = :uid AND message.timestamp > notifmger.lastaccess AND message.type != :type", array(
        ":uid" => $user->uid,
        ":type" => "user_tagged_self"))->fetchCol();
    if ($msgs[0] != 0){
        return $msgs[0];
    }
    return NULL;
}
1

Have a look at my answer to "How to allow users to manage their own Message Stack messages?", which IMO is pretty close to the question here. It can be summarized like so:

  1. Create a flag (using the the Flag module).
  2. Create a view 'Messages by user'.
  3. Use Rules to flag/unflag messages.
  4. Trigger the Rules Component to mark messages (using the VBO module).
  5. Create notifications about Unread Messages (using the "Menu Badges" module).

Note: no custom coding involved, only using some of the commonly used contributed modules (and "Menu Badges", a hidden gem ...).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.