How to notify online users of a new private message

Last modified: August 23, 2009 - 22:29

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;
?>

Block Message

Dokuro - February 18, 2009 - 05:57

Here is something quick I came up with for the block area.

<?php
 
global $user;
 
  if ((
$user-uid) && (_privatemsg_get_new_messages($user->uid) != 0)){
     
$newmess = _privatemsg_get_new_messages($user->uid);
      echo
'<a href="/privatemsg/inbox">You have ' . $newmess . ' message(s)!</a>';
  }
?>

Fatal error

manop - April 18, 2009 - 22:46

I tried the code about in the block. I got

Fatal error: Call to undefined function _privatemsg_get_new_messages() in .../includes/common.inc(1645) : eval()'d code on line 4

How do I get the new messages?

Example for Drupal 6

Keyz - April 19, 2009 - 11:49

If you're using Drupal 6, this is apparently changed.

What I use is something like:

<?php
 
global $user;
 
$new_pm_count = privatemsg_unread_count($account = NULL);
  print
'Messages '. l('('. $new_pm_count .' new)', 'messages');
?>

Which makes something like (2 new being a link to /messages):
Messages (2 new)

This isn't quite what mine is... mine's actually part of a larger chunk of code and other features which already handles checking if the user is logged in, etc, so you'd have to add that part in. I basically found the privatemsg_unread_count() function within PM module and used it here.

 
 

Drupal is a registered trademark of Dries Buytaert.