Hi.
Current push_notifications_privatemsg_message_insert is incomplete, because if role is selected for a private message, there are no recipients to send push notification.
This code fix this function: (Sorry, I cannot create a patch file).
File: push_notification.module
Function: push_notifications_privatemsg_message_insert($message)
New function:

<?php
/**
 * Implements hook_privatemsg_message_insert.
 */
function push_notifications_privatemsg_message_insert( $message ) {

  if ( variable_get( 'push_notifications_privatemsg_integration', 0 ) ) {
    // Compose the payload. If the body is empty, just use the subject line.
    // Otherwise, combine subject and body.
    $payload = (empty($message->body)) ? $message->subject : $message->subject . ' ' . $message->body;
    $payload = 'From ' . $message->author->name . ': ' . $payload;

    // Compose an array of recipients.
    $recipients = array();
    foreach ( $message->recipients as $recipient ) {
      if ( $recipient->type == "role" && $recipient->name != 'authenticated user' ) {
        $sql = 'SELECT uid FROM {users_roles} WHERE rid = :rid';
        $q = db_query( $sql, array( ':rid' => $recipient->rid ) );
        //  $uids = array();
        //  while ($row = db_fetch_array($q)) {
        foreach ( $q as $row ) {
          //    $uids[] = $row['uid'];
          $recipients[] = $row->uid;
        }
      }
      elseif ( $recipient->type == "role" && $recipient->name == 'authenticated user' ) {
        $q = db_select( 'users', 'u' )
        ->fields( 'u' )
        ->execute()
        ->fetchAssoc();
        foreach ( $q as $row ) {
          $recipients[] = $row->uid;
        }
      }
      else
        $recipients[] = $recipient->uid;
    }


    push_notifications_send_message( $recipients, $payload );
  }
}

?>

I cannot send my actual file, because I did many other changes.
Regards.

Byron H.
http://silencesoft.pw
pd. Thanks if somebody can create a patch with this code.

Comments

Anonymous’s picture

I'm just about to test this on a project. I'll create a patch shortly.

Best, Paul

Anonymous’s picture

I have cleaned up the code and modified the code to consistently use the latest database abstraction layer.

Will test soon.

/**
 * Implements hook_privatemsg_message_insert.
 */
function push_notifications_privatemsg_message_insert( $message ) {
  if ( variable_get( 'push_notifications_privatemsg_integration', 0 ) ) {
    // Compose the payload. If the body is empty, just use the subject line.
    // Otherwise, combine subject and body.
    $payload = (empty($message->body)) ? $message->subject : $message->subject . ' ' . $message->body;
    $payload = 'From ' . $message->author->name . ': ' . $payload;
    // Compose an array of recipients.
    $recipients = array();
    
    foreach ( $message->recipients as $recipient ) { 
      if ( $recipient->type == "role" && $recipient->name != 'authenticated user' ) {   
        $results = db_select('users_roles', 'ur')
        ->fields('ur', array('uid'))
				->condition('ur.rid', $recipient->rid, '=')
        ->execute()
        ->fetchCol();
      }
      elseif ( $recipient->type == "role" && $recipient->name == 'authenticated user' ) {   
        $results = db_select('users', 'u')
        ->fields( 'u' )
        ->execute()
        ->fetchCol();
      }  
      if ($recipient->type == "role") {
        foreach ($results as $result) {
          $recipients[] = $result;
        }
      }
      else{
        $recipients[] = $recipient->uid; watchdog('debug', '<pre>'. print_r($recipient, TRUE) .'</pre>');

      }  
    }
    push_notifications_send_message( $recipients, $payload );
  }
}
Anonymous’s picture

The above works great. I'll create a patch ..

jbarwick’s picture

I noticed that paul said he would create a patch 7 months ago.

The latest dev release says 11 months ago.

So, I assume the patch is not in the dev branch.

OK, so the question is, do we apply the above subroutine to the prod branch or the dev branch?

jbarwick’s picture

By the way, I just applied to prod branch and it worked great.

Should we be using dev branch? anyone know?

silenceway’s picture

Hello.
My code works on dev and stable. But I'm using dev at this time.
Paul code must works on both two.
:)
pd. Thanks Paul for update.

Byron H.
http://silencesoft.pw

  • haagendazs committed e3a27dd on 7.x-1.x authored by silenceway
    Issue #2206009 by paulbooker, silenceway: Push Notification does not...
haagendazs’s picture

Status: Active » Fixed

Just pushed the patch from #2 to the dev branch. Thanks for the patch!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.