ForkAPI::applePush PHP Method

applePush() public method

Push a notification to apple
Deprecation: : no more support for the Fork-app.
public applePush ( mixed $deviceTokens, mixed $alert, int[optional] $badge = null, string[optional] $sound = null, array $extraDictionaries = null ) : array
$deviceTokens mixed The device token(s) for the receiver.
$alert mixed The message/dictonary to send.
$badge int[optional]
$sound string[optional]
$extraDictionaries array
return array The device tokens that aren't valid.
    public function applePush($deviceTokens, $alert, $badge = null, $sound = null, array $extraDictionaries = null)
    {
        trigger_error('applePush is deprecated since the Fork-app is not maintained anymore.', E_USER_DEPRECATED);
        // build parameters
        $parameters['device_token'] = (array) $deviceTokens;
        $parameters['alert'] = $alert;
        if ($badge !== null) {
            $parameters['badge'] = (int) $badge;
        }
        if ($sound !== null) {
            $parameters['sound'] = (string) $sound;
        }
        if ($extraDictionaries !== null) {
            $parameters['extra_dictionaries'] = $extraDictionaries;
        }
        // make the call
        $response = $this->doCall('apple.push', $parameters, true, true);
        // validate
        if (!isset($response->failed_device_tokens)) {
            throw new ForkAPIException('Invalid XML-response.');
        }
        // init var
        $return = array();
        // available devices?
        if (isset($response->failed_device_tokens->device)) {
            // loop and add to return
            foreach ($response->failed_device_tokens->device as $device) {
                $return[] = (string) $device['token'];
            }
        }
        // return
        return $return;
    }

Usage Example

示例#1
0
 /**
  * Push a notification to Apple's notifications-server
  *
  * @param mixed  $alert             The message/dictionary to send.
  * @param int    $badge             The number for the badge.
  * @param string $sound             The sound that should be played.
  * @param array  $extraDictionaries Extra dictionaries.
  */
 public static function pushToAppleApp($alert, $badge = null, $sound = null, array $extraDictionaries = null)
 {
     // get ForkAPI-keys
     $publicKey = self::get('fork.settings')->get('Core', 'fork_api_public_key', '');
     $privateKey = self::get('fork.settings')->get('Core', 'fork_api_private_key', '');
     // no keys, so stop here
     if ($publicKey == '' || $privateKey == '') {
         return;
     }
     // get all apple-device tokens
     $deviceTokens = (array) self::getContainer()->get('database')->getColumn('SELECT s.value
          FROM users AS i
          INNER JOIN users_settings AS s
          WHERE i.active = ? AND i.deleted = ? AND s.name = ? AND s.value != ?', array('Y', 'N', 'apple_device_token', 'N;'));
     // no devices, so stop here
     if (empty($deviceTokens)) {
         return;
     }
     // init var
     $tokens = array();
     // loop devices
     foreach ($deviceTokens as $row) {
         // unserialize
         $row = unserialize($row);
         // loop and add
         foreach ($row as $item) {
             $tokens[] = $item;
         }
     }
     // no tokens, so stop here
     if (empty($tokens)) {
         return;
     }
     // require the class
     require_once PATH_LIBRARY . '/external/fork_api.php';
     // create instance
     $forkAPI = new \ForkAPI($publicKey, $privateKey);
     try {
         // push
         $response = $forkAPI->applePush($tokens, $alert, $badge, $sound, $extraDictionaries);
         if (!empty($response)) {
             // get db
             $db = self::getContainer()->get('database');
             // loop the failed keys and remove them
             foreach ($response as $deviceToken) {
                 // get setting wherein the token is available
                 $row = $db->getRecord('SELECT i.*
                      FROM users_settings AS i
                      WHERE i.name = ? AND i.value LIKE ?', array('apple_device_token', '%' . $deviceToken . '%'));
                 // any rows?
                 if (!empty($row)) {
                     // reset data
                     $data = unserialize($row['value']);
                     // loop keys
                     foreach ($data as $key => $token) {
                         // match and unset if needed.
                         if ($token == $deviceToken) {
                             unset($data[$key]);
                         }
                     }
                     // no more tokens left?
                     if (empty($data)) {
                         $db->delete('users_settings', 'user_id = ? AND name = ?', array($row['user_id'], $row['name']));
                     } else {
                         $db->update('users_settings', array('value' => serialize($data)), 'user_id = ? AND name = ?', array($row['user_id'], $row['name']));
                     }
                 }
             }
         }
     } catch (Exception $e) {
         if (self::getContainer()->getParameter('kernel.debug')) {
             throw $e;
         }
     }
 }