You are here

function push_notifications_mass_push_form_submit in Push Notifications 7

Submit handler for sending out a mass-push notification.

File

includes/push_notifications.admin.inc, line 503
Admin files for Push Notifications module.

Code

function push_notifications_mass_push_form_submit($form, &$form_state) {
  $recipients = $form_state['values']['recipients'];
  $payload = $form_state['values']['payload'];
  $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(array(
      'type_id' => PUSH_NOTIFICATIONS_TYPE_ID_IOS,
      'language' => $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(array(
      'type_id' => PUSH_NOTIFICATIONS_TYPE_ID_ANDROID,
      'language' => $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;
        case PUSH_NOTIFICATIONS_GOOGLE_TYPE_FCM:
          $result = push_notifications_fcm_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.'));
    }
  }
}