Updating push notification page to send out messages to devices belonging to users that have a given role
/**
* Additional handler for push_notifications_mass_push_form_validate form validate.
*/
function mymodule_push_notifications_mass_push_form_validate($form, &$form_state) {
}
/**
* Additional handler for push_notifications_mass_push_form_submit form submit.
*/
function mymodule_push_notifications_mass_push_form_submit($form, &$form_state) {
$recipients = $form_state['values']['recipients'];
$payload = $form_state['values']['payload'];
$role = $form_state['values']['role'];
$language = (isset($form_state['values']['language'])) ? $form_state['values']['language'] : false;
// Send message to all iOS recipients.
if (!empty($recipients['ios'])) {
// Get all iOS recipients.
$tokens_ios = push_notifications_get_tokens_by_role(PUSH_NOTIFICATIONS_TYPE_ID_IOS, $role, $language);
if (!empty($tokens_ios)) {
// Convert the payload into the correct format for APNS.
$payload_apns = array('aps' => $payload);
$result = push_notifications_apns_send_message($tokens_ios, $payload_apns);
$dsm_type = ($result['success']) ? 'status' : 'error';
drupal_set_message($result['message'], $dsm_type);
}
else {
drupal_set_message(t('No iOS recipients found, potentially for this language.'));
}
}
// Send message to all Android recipients.
if (!empty($recipients['android'])) {
// Get all Android recipients.
$tokens_android = push_notifications_get_tokens_by_role(PUSH_NOTIFICATIONS_TYPE_ID_ANDROID, $role, $language);
if (!empty($tokens_android)) {
// Determine which method to use for Google push notifications.
switch (PUSH_NOTIFICATIONS_GOOGLE_TYPE) {
case PUSH_NOTIFICATIONS_GOOGLE_TYPE_C2DM:
$result = push_notifications_c2dm_send_message($tokens_android, $payload);
break;
case PUSH_NOTIFICATIONS_GOOGLE_TYPE_GCM:
$result = push_notifications_gcm_send_message($tokens_android, $payload);
break;
}
$dsm_type = ($result['success']) ? 'status' : 'error';
drupal_set_message($result['message'], $dsm_type);
}
else {
drupal_set_message(t('No Android recipients found, potentially for this language.'));
}
}
}
/**
* Determine all recipients of a given role from a specific device type.
*
* @param $type_id
* Device Type ID.
* @param $role
* User Role.
* @param $language
* Language code, optional.
* @param $raw
* Boolean, set true to retrieve the raw query results.
*
* @return
* Array of results, null if no entries.
*/
function push_notifications_get_tokens_by_role($type_id = '', $role = FALSE, $language = FALSE, $raw = FALSE) {
// Make sure this type_id is supported.
$valid_type_ids = array(PUSH_NOTIFICATIONS_TYPE_ID_IOS, PUSH_NOTIFICATIONS_TYPE_ID_ANDROID);
if (!in_array($type_id, $valid_type_ids)) {
return FALSE;
}
// Select all tokens for this type id and users of given user role.
$query = db_select('push_notifications_tokens', 'pnt');
$query->join('users', 'u', 'pnt.uid = u.uid');
$query->join('users_roles', 'ur', 'u.uid = ur.uid');
$query->fields('pnt', array('token'));
$query->condition('pnt.type', $type_id);
$query->condition('ur.rid', $role);
// If language code is passed, limit the results by language.
if ($language) {
$query->condition('pnt.language', $language);
}
$result = $query->execute();
// Return raw result, if needed.
if ($raw) {
return $result;
}
// Otherwise, create an array of tokens.
else {
$tokens = array();
foreach ($result as $record) {
$tokens[] = $record->token;
}
return $tokens;
}
}
/**
* Implements hook_form_alter().
*/
function mymodule_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'push_notifications_mass_push_form') {
$result = db_select('role', 'r')
->fields('r',array('rid','name'))
->condition('name',array('administrator','anonymous user'),'NOT IN')
->execute()
->fetchAll();
$role = array();
foreach($result as $role){
$roles[$role->rid] = $role->name;
}
$form['role']['#title'] = "Roles";
$form['role']['#type'] = "select";
$form['role']['#options'] = $roles;
$form['role']['#required'] = 1;
$form['#validate'][] = 'mymodule_push_notifications_mass_push_form_validate';
unset($form['#submit']);
$form['#submit'][] = 'mymodule_push_notifications_mass_push_form_submit';
}
}
Tags: