You are here

function push_notifications_store_token in Push Notifications 7

Store a token in the database. Removes all spaces in the token.

Parameters

$token: Device token.

$type_id: Device type id.

$uid: User ID.

$language: Language that this token is registered for, optional.

Return value

Failure to write a record will return FALSE, Otherwise SAVED_NEW.

1 call to push_notifications_store_token()
_push_notifications_service_create_device_token in includes/push_notifications.service.inc
Service callback to store a device token.

File

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

Code

function push_notifications_store_token($token = '', $type_id = '', $uid = '', $language = '') {

  // Let modules modify the token before it is saved to the database.
  foreach (module_implements('push_notifications_store_token') as $module) {
    $function = $module . '_push_notifications_store_token';
    $function($token, $type_id, $uid);
  }

  // Invoke rules.
  if (module_exists('rules')) {
    rules_invoke_event_by_args('push_notifications_before_token_insert', array(
      'token' => $token,
      'type_id' => $type_id,
      'uid' => $uid,
      'language' => $language,
    ));
  }
  if (!is_string($token) || !is_numeric($type_id) || !is_numeric($uid)) {
    return FALSE;
  }

  // Default language to site default.
  if ($language == '') {
    $default_language = language_default();
    $language = $default_language->language;
  }

  // Write record.
  $table = 'push_notifications_tokens';
  $record = new stdClass();
  $record->token = $token;
  $record->uid = $uid;
  $record->type = $type_id;
  $record->language = $language;
  $record->timestamp = time();
  $result = drupal_write_record($table, $record);

  // Allow modules to react to the newly created record after creation.
  foreach (module_implements('push_notifications_post_store_token') as $module) {
    $function = $module . '_push_notifications_post_store_token';
    $function($record);
  }

  // Invoke rules.
  if (module_exists('rules')) {
    rules_invoke_event_by_args('push_notifications_after_token_insert', array(
      'token' => $token,
      'type_id' => $type_id,
      'uid' => $uid,
      'language' => $language,
      'result' => $result,
    ));
  }
  return $result;
}