You are here

push_notifications.module in Push Notifications 8

Same filename and directory in other branches
  1. 7 push_notifications.module

Contains push_notifications.module functionality.

File

push_notifications.module
View source
<?php

/**
 * @file
 * Contains push_notifications.module functionality.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Link;

/**
 * OS device network IDs.
 */
const PUSH_NOTIFICATIONS_NETWORK_ID_IOS = 'apns';
const PUSH_NOTIFICATIONS_NETWORK_ID_ANDROID = 'gcm';

/**
 * APNS feedback host.
 */
const PUSH_NOTIFICATIONS_APNS_FEEDBACK_HOST = 'feedback.push.apple.com';

/**
 * APNS feedback port.
 */
const PUSH_NOTIFICATIONS_APNS_FEEDBACK_PORT = 2196;

/**
 * APNS size limit for individual payload, in bytes.
 */
const PUSH_NOTIFICATIONS_APNS_PAYLOAD_SIZE_LIMIT = 2048;

/**
 * Implements hook_help().
 */
function push_notifications_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the push_notifications module.
    case 'help.page.push_notifications':
      $PushHandler = \Drupal::service('push_notifications.message_sender_accounts');
      $PushHandler
        ->setAccounts(array(
        Drupal::currentUser(),
      ));
      $PushHandler
        ->setMessage('This is my test payload.');
      $PushHandler
        ->dispatch();
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Push notification functionality for iPhone and Android.') . '</p>';
      $output .= '<p>' . t('The full documentation is available online: <a href="@doc_url" target="_blank">Documentation</a >.', array(
        '@doc_url' => 'https://www.drupal.org/node/2718063',
      )) . '</p>';
      $output .= '<p>' . t('You can manually send out a push notification @message_link.', array(
        '@message_link' => Link::createFromRoute('from the admin section', 'push_notifications.send_message_form')
          ->toString(),
      )) . '</p>';
      return $output;
  }
}

/**
 * Get available networks.
 */
function push_notifications_get_networks() {
  return array(
    PUSH_NOTIFICATIONS_NETWORK_ID_IOS,
    PUSH_NOTIFICATIONS_NETWORK_ID_ANDROID,
  );
}

/**
 * Generate and set the random file ending for APNS certificates.
 */
function push_notifications_set_random_certificate_string() {

  // Generate a random 10-digit string.
  $random_string = substr(md5(microtime()), 0, 10);

  // Write random string to variables.
  $config = \Drupal::service('config.factory')
    ->getEditable('push_notifications.apns');
  $config
    ->set('certificate_random', $random_string);
  $config
    ->save();
}

/**
 * Get name of random certificate.
 *
 * @param string $environment
 *   Environment name.
 *
 * @return string
 *   Certificate filename.
 */
function push_notifications_get_certificate_name($environment = 'development') {
  $filename = array(
    'apns',
    $environment,
    \Drupal::config('push_notifications.apns')
      ->get('certificate_random'),
  );
  return implode('-', $filename) . '.pem';
}

/**
 * Determine if this user has already stored a token
 * in the database. The same device token can be
 * registered for multiple users, because multiple
 * users can login from the same device.
 *
 * @param $token
 *   Device Token.
 * @param $uid
 *   User ID.
 * @param $exclude
 *   Set this to true to find (at least one) other user(s) who have this
 *   token stored. Optional, defaults to false.
 *
 * @return
 *   User ID of token, if found.
 */
function push_notifications_find_token($token = '', $uid = '', $exclude = FALSE) {
  if ($token == '') {
    return FALSE;
  }
  $query = db_select('push_notifications_tokens', 'pnt');
  $query
    ->fields('pnt', array(
    'token',
  ));
  $query
    ->condition('pnt.token', $token);
  if ($exclude) {
    $query
      ->condition('pnt.uid', $uid, '!=');
    $query
      ->range(0, 1);
  }
  else {
    $query
      ->condition('pnt.uid', $uid);
  }
  $result = $query
    ->execute();
  return $result
    ->fetchField();
}

/**
 * Determine all recipients from a specific network.
 *
 * @param array $filters Filter option for query. Allows filtering by:
 *   - Language (key = language)
 *   - Networks (key = networks, value = array)
 *   - Account type (key = account_type)
 * @param $raw
 *   Boolean, set true to retrieve the raw query results.
 *
 * @return mixed Array of results, null if no entries.
 */
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 network, if required.
  if (array_key_exists('networks', $filters)) {
    $query
      ->condition('pnt.network', $filters['networks'], 'IN');
  }

  // 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;
    }
    return $tokens;
  }
}

/**
 * Determine all tokens for a specfic user.
 *
 * @param int $uid User ID.
 * @return array Array of token database records.
 *
 */
function push_notification_get_user_tokens($uid) {
  if (!is_numeric($uid)) {
    return FALSE;
  }

  // Select all tokens for this user.
  $query = db_select('push_notifications_tokens', 'pnt');
  $query
    ->fields('pnt');
  $query
    ->condition('pnt.uid', $uid);
  $result = $query
    ->execute();
  $tokens = array();
  foreach ($result as $record) {
    $tokens[$record->token] = $record;
  }
  return $tokens;
}

Functions

Namesort descending Description
push_notifications_find_token Determine if this user has already stored a token in the database. The same device token can be registered for multiple users, because multiple users can login from the same device.
push_notifications_get_certificate_name Get name of random certificate.
push_notifications_get_networks Get available networks.
push_notifications_get_tokens Determine all recipients from a specific network.
push_notifications_help Implements hook_help().
push_notifications_set_random_certificate_string Generate and set the random file ending for APNS certificates.
push_notification_get_user_tokens Determine all tokens for a specfic user.

Constants

Namesort descending Description
PUSH_NOTIFICATIONS_APNS_FEEDBACK_HOST APNS feedback host.
PUSH_NOTIFICATIONS_APNS_FEEDBACK_PORT APNS feedback port.
PUSH_NOTIFICATIONS_APNS_PAYLOAD_SIZE_LIMIT APNS size limit for individual payload, in bytes.
PUSH_NOTIFICATIONS_NETWORK_ID_ANDROID
PUSH_NOTIFICATIONS_NETWORK_ID_IOS OS device network IDs.