How to notify online users of a new private message

Note that this functionality is now built in to the module (as of Jan. 26, 2007).

The module will send an email when you have a new private message, but it's nice to have an on screen notification as well. The following snippit can be placed in your theme. Mailbox icons can be found on the web or you can delete that portion of the snippit.

*WARNING* If you decide to disable the private message module, be sure to take this out of your theme as it will not find the function and will error.

<?php
 
global $user;
  if (
$user->uid) {
   
$numnew = _privatemsg_get_new_messages($user->uid);
    if (
$numnew > 0) {
    print
l(theme('image', path_to_theme().'/newmail.gif'),'privatemsg',
        array(
'title' =>'You have ' . $numnew . ' new message(s)'),NULL, NULL, NULL, TRUE);
    }
  }
?>

(I think this worked as is in 4.6 as well but no longer have that installed to confirm)

Block method (thanks Steel Rat):
Create a new block with your message and use the PHP visibility option with this code:

<?php
 
global $user;
 
$newmess = FALSE;
  if (
$user->uid) {
   
$newmess = _privatemsg_get_new_messages($user->uid) > 0;
  }

  return
$newmess;
?>

 
 

Drupal is a registered trademark of Dries Buytaert.