' . t('Send Notification The EasyAPNS module allows users to send notifications on iphone.') . '

'; return $output; } } /** * Implementation of hook_menu(). */ function iphone_push_notification_through_easyapns_menu() { $items = array(); // added to call from url : created in order to test notification from admin panel $items['pushnotif/%/%/%'] = array( 'title' => 'Push Notification', 'description' => 'Send test Push Notification', 'page callback' => 'iphone_push_notification_through_easyapns_push_notify', 'page arguments' => array(1, 2, 3), // uid, message , parameters (key1:value1|key2:value2|key3:value3) for eg. pushnotif/userid/hellotest/type:NH 'access arguments' => array('administer easyapns'), 'type' => MENU_NORMAL_ITEM, ); $items['admin/settings/easyapns'] = array( 'title' => 'Easy Apple Push Notification', 'description' => 'Easy Apple Push Notification settings page', 'page callback' => 'drupal_get_form', 'page arguments' => array('iphone_push_notification_through_easyapns_admin_form'), 'access arguments' => array('administer easyapns'), 'type' => MENU_NORMAL_ITEM, ); return $items; } /** * iphone_push_notification_through_easyapns_admin_form is used to create form in admin and set the basic requirement for sending * notifications on device */ function iphone_push_notification_through_easyapns_admin_form() { $options = array('enabled' => 'enabled', 'disabled' => 'disabled'); $form = array(); $form['iphone_push_notification_through_easyapns_appname'] = array( '#type' => 'textfield', '#title' => t('Application Name'), '#default_value' => variable_get('iphone_push_notification_through_easyapns_appname', ''), '#size' => 10, '#maxlength' => 255, '#description' => t("The Application name for which notification has to be sent."), '#required' => TRUE, ); $form['iphone_push_notification_through_easyapns_appversion'] = array( '#type' => 'textfield', '#title' => t('Application Version'), '#default_value' => variable_get('iphone_push_notification_through_easyapns_appversion', ''), '#size' => 10, '#maxlength' => 255, '#description' => t("The Application Version."), '#required' => TRUE, ); $form['iphone_push_notification_through_easyapns_devicename'] = array( '#type' => 'textfield', '#title' => t('Device Name'), '#default_value' => variable_get('iphone_push_notification_through_easyapns_devicename', ''), '#size' => 10, '#maxlength' => 255, '#description' => t("The Name of device(iphone,ipad etc)."), '#required' => TRUE, ); $form['iphone_push_notification_through_easyapns_devicemodel'] = array( '#type' => 'textfield', '#title' => t('Device Model'), '#default_value' => variable_get('iphone_push_notification_through_easyapns_devicemodel', ''), '#size' => 10, '#maxlength' => 255, '#description' => t("The Model of device."), '#required' => TRUE, ); $form['iphone_push_notification_through_easyapns_deviceuid'] = array( '#type' => 'textfield', '#title' => t('Device uid'), '#default_value' => variable_get('iphone_push_notification_through_easyapns_deviceuid', '12345'), '#size' => 10, '#maxlength' => 255, '#description' => t("The uid of device."), '#required' => TRUE, ); $form['iphone_push_notification_through_easyapns_deviceversion'] = array( '#type' => 'textfield', '#title' => t('Device Version'), '#default_value' => variable_get('iphone_push_notification_through_easyapns_version', '4.0'), '#size' => 10, '#maxlength' => 255, '#description' => t("The Version of device."), '#required' => TRUE, ); $form['iphone_push_notification_through_easyapns_allow_badge'] = array( '#type' => 'select', '#title' => t('Allow Badge'), '#default_value' => variable_get('iphone_push_notification_through_easyapns_allow_badge', '1'), '#description' => t("Allow sending badge with push notification."), '#options' => $options, '#default_value' => 'enabled', ); $form['iphone_push_notification_through_easyapns_allow_pushalert'] = array( '#type' => 'select', '#title' => t('Allow Push alert'), '#description' => t("Allow push alert for push notification."), '#options' => $options, '#default_value' => 'enabled', '#required' => FALSE, ); $form['iphone_push_notification_through_easyapns_allow_pushsound'] = array( '#type' => 'select', '#title' => t('Allow Push Sound'), '#description' => t("Allow push Sound for push notification."), '#options' => $options, '#default_value' => 'enabled', ); $form['iphone_push_notification_through_easyapns_production_certificate'] = array( '#type' => 'textfield', '#title' => t('Path of production Certificate'), '#default_value' => variable_get('iphone_push_notification_through_easyapns_production_certificate', 'complete path of production certificate here'), '#description' => t("Give path of productio certificate here."), '#required' => FALSE, ); $form['iphone_push_notification_through_easyapns_sandbox_certificate'] = array( '#type' => 'textfield', '#title' => t('Path of sandbox Certificate'), '#default_value' => variable_get('iphone_push_notification_through_easyapns_sandbox_certificate', 'complete path of sandbox certificate here'), '#description' => t("Give path of sandbox certificate here."), '#required' => FALSE, ); return system_settings_form($form); } /** * Implementation of hook_user(). * case : insert => used for inserting device id of logged in user into apns_devices table * so that notifications can be send to the user */ function iphone_push_notification_through_easyapns_user($op, &$edit, &$account) { switch ($op) { case 'insert': $appid = addslashes($_REQUEST['field_apple_id'][0]['value']); iphone_push_notification_through_easyapns_apns_insert($account->uid, $appid); break; case 'update': break; case 'delete': db_result(db_query("DELETE FROM {apns_devices} WHERE pid = " . $account->uid)); break; } } /** * Implementation of hook_nodeapi(). */ function iphone_push_notification_through_easyapns_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { if (is_content_profile($node) && $op == 'update') { $appid = addslashes($_REQUEST['field_apple_id'][0]['value']); iphone_push_notification_through_easyapns_apns_update($node->uid, $appid); } } /** * iphone_push_notification_through_easyapns_push_notify == main function to send notification to the iphone, * on the basis of device_token * */ function iphone_push_notification_through_easyapns_push_notify($uid, $msg = '', $paramAry = NULL) { $moduleFilePath = dirname(__FILE__); require_once($moduleFilePath . '/ApnsPHP/Autoload.php'); $prod_certificate = variable_get('iphone_push_notification_through_easyapns_production_certificate', ''); $sandbox_certificate = variable_get('iphone_push_notification_through_easyapns_sandbox_certificate', ''); $push = new ApnsPHP_Push( ApnsPHP_Abstract::ENVIRONMENT_SANDBOX, $sandbox_certificate ); $push->connect(); $device_id = db_result(db_query("SELECT devicetoken FROM apns_devices WHERE pid = %d", $uid)); $message = new ApnsPHP_Message($device_id); $message->setCustomIdentifier("Message-Badge-3"); $message->setBadge(3); $message->setText($msg); $message->setSound(); $message->setCustomProperty('acme2', array('bang', 'whiz')); $message->setCustomProperty('acme3', array('bing', 'bong')); if (!is_array($paramAry) AND $paramAry) { $aryP = explode("|", $paramAry); $paramAry = array(); if ($aryP) { foreach ($aryP as $v) { $aryV = explode(":", $v); // getting parameter key and value $paramAry = array_merge($paramAry, array($aryV[0] => $aryV[1])); } } } if ($paramAry AND is_array($paramAry)) { foreach ($paramAry as $k => $v) { $message->setCustomProperty($k, $v); } } $message->setExpiry(30); $push->add($message); $push->send(); $push->disconnect(); return 'SUCCESS'; } /** * function callled above in order to make an entry in the apns_devices table for proper sending of notification */ function iphone_push_notification_through_easyapns_apns_insert($uid, $appid, $device_name='', $device_model='', $device_version='') { $sound = variable_get('iphone_push_notification_through_easyapns_allow_pushsound', ''); $appname = variable_get('iphone_push_notification_through_easyapns_appname', ''); $appversion = variable_get('iphone_push_notification_through_easyapns_appversion', ''); $device_name = (($device_name == '') ? variable_get('iphone_push_notification_through_easyapns_devicename', '') : $device_name); $device_model = variable_get('iphone_push_notification_through_easyapns_devicemodel', ''); $device_version = variable_get('iphone_push_notification_through_easyapns_deviceversion', ''); $allow_badge = variable_get('iphone_push_notification_through_easyapns_allow_badge', ''); $allow_pushalert = variable_get('iphone_push_notification_through_easyapns_allow_pushalert', ''); $allow_pushsound = variable_get('iphone_push_notification_through_easyapns_allow_pushsound', ''); $prod_certificate = variable_get('iphone_push_notification_through_easyapns_production_certificate', ''); $sandbox_certificate = variable_get('iphone_push_notification_through_easyapns_sandbox_certificate', ''); $device_uid = ''; db_query("INSERT INTO {apns_devices} (`pid`, `appname`, `appversion`, `deviceuid`, `devicetoken`, `devicename`, `devicemodel`, `deviceversion`, `pushbadge`, `pushalert`, `pushsound`, `development`, `status`, `created`, `modified`) VALUES ('" . $uid . "', '" . $appname . "', '" . $appversion . "', '" . $device_uid . "', '" . $appid . "', '" . $device_name . "', '" . $device_model . "' , '" . $device_version . "', '" . $allow_badge . "', '" . $allow_pushalert . "', '" . $allow_pushsound . "', 'production', 'active', '" . date('Y-m-d H:i:s') . "', '')"); } /** * Function required in order to update device token * of user in case of login from different device */ function iphone_push_notification_through_easyapns_apns_update($uid, $appid) { $sql = "UPDATE {apns_devices} SET devicetoken = '" . $appid . "' WHERE pid = '" . $uid . "'"; db_query($sql); }