View source
<?php
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Link;
const PUSH_NOTIFICATIONS_NETWORK_ID_IOS = 'apns';
const PUSH_NOTIFICATIONS_NETWORK_ID_ANDROID = 'gcm';
const PUSH_NOTIFICATIONS_APNS_FEEDBACK_HOST = 'feedback.push.apple.com';
const PUSH_NOTIFICATIONS_APNS_FEEDBACK_PORT = 2196;
const PUSH_NOTIFICATIONS_APNS_PAYLOAD_SIZE_LIMIT = 2048;
function push_notifications_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
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;
}
}
function push_notifications_get_networks() {
return array(
PUSH_NOTIFICATIONS_NETWORK_ID_IOS,
PUSH_NOTIFICATIONS_NETWORK_ID_ANDROID,
);
}
function push_notifications_set_random_certificate_string() {
$random_string = substr(md5(microtime()), 0, 10);
$config = \Drupal::service('config.factory')
->getEditable('push_notifications.apns');
$config
->set('certificate_random', $random_string);
$config
->save();
}
function push_notifications_get_certificate_name($environment = 'development') {
$filename = array(
'apns',
$environment,
\Drupal::config('push_notifications.apns')
->get('certificate_random'),
);
return implode('-', $filename) . '.pem';
}
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();
}
function push_notifications_get_tokens($filters = array(), $raw = FALSE) {
if (!is_array($filters)) {
return FALSE;
}
$query = db_select('push_notifications_tokens', 'pnt');
$query
->fields('pnt');
if (array_key_exists('networks', $filters)) {
$query
->condition('pnt.network', $filters['networks'], 'IN');
}
if (array_key_exists('language', $filters) && is_string($filters['language'])) {
$query
->condition('pnt.language', $filters['language']);
}
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();
if ($raw) {
return $result;
}
else {
$tokens = array();
foreach ($result as $record) {
$tokens[] = $record;
}
return $tokens;
}
}
function push_notification_get_user_tokens($uid) {
if (!is_numeric($uid)) {
return FALSE;
}
$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;
}