Sending Push Notifications to users with a given role using Private Messages
/**
* 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;
}
}
push_notifications_send_message( $recipients, $payload );
}
}
Tags:
URL: Push Notification does not send Push when Private Message goes to Roles
Tweet