You are here

function push_notifications_get_tokens in Push Notifications 7

Same name and namespace in other branches
  1. 8 push_notifications.module \push_notifications_get_tokens()

Determine all recipients from a specific device type.

Parameters

array $filters Filter option for query. Allows filtering by::

  • Language (key = language)
  • Device type (key = type_id)
  • Account type (key = account_type)

$raw: Boolean, set true to retrieve the raw query results.

Return value

mixed Array of results, null if no entries.

2 calls to push_notifications_get_tokens()
push_notifications_mass_push_form_submit in includes/push_notifications.admin.inc
Submit handler for sending out a mass-push notification.
push_notifications_send_message_bulk in ./push_notifications.module
Send a simple message alert to all tokens in the database.

File

./push_notifications.module, line 1138
Push Notifications functionality.

Code

function push_notifications_get_tokens($filters = array(), $raw = FALSE) {

  // Validate format of filters argument.
  if (!is_array($filters)) {
    return FALSE;
  }

  // Select all tokens for this type id.
  $query = db_select('push_notifications_tokens', 'pnt');
  $query
    ->fields('pnt');

  // Filter by device type, if required.
  if (array_key_exists('type_id', $filters)) {
    $query
      ->condition('pnt.type', $filters['type_id']);
  }

  // Filter by language, if required.
  if (array_key_exists('language', $filters) && is_string($filters['language'])) {
    $query
      ->condition('pnt.language', $filters['language']);
  }

  // Filter by anonymous vs. authenticated users.
  if (array_key_exists('account_type', $filters) && in_array($filters['account_type'], array(
    'anonymous',
    'authenticated',
  ))) {
    switch ($filters['account_type']) {
      case 'anonymous':
        $query
          ->condition('pnt.uid', 0);
        break;
      case 'authenticated':
        $query
          ->condition('pnt.uid', 0, '!=');
        break;
    }
  }
  $result = $query
    ->execute();

  // Return raw result, if needed.
  if ($raw) {
    return $result;
  }
  else {
    $tokens = array();
    foreach ($result as $record) {
      $tokens[] = $record->token;
    }
    return $tokens;
  }
}