Пример #1
0
 /**
  * Send message to devices
  *
  * @param string $message
  * @param Device[] $devices
  * @param array  $custom_fields
  */
 public function send($message, $devices, $custom_fields = array())
 {
     // If list of devices is empty, then we must terminate this process
     if (empty($devices)) {
         return;
     }
     // Passphrase in default is phpush
     $passphrase = 'phpush';
     // If we setting passphrase in custom fields, then change it and delete from array
     if (array_key_exists('passphrase', $custom_fields)) {
         $passphrase = $custom_fields['passphrase'];
         unset($custom_fields['passphrase']);
     }
     // Create the payload body
     $body['aps'] = array_merge(array('alert' => $message, 'sound' => 'default'), $custom_fields);
     // Encode the payload as JSON
     $payload = json_encode($body);
     // For each device
     foreach ($devices as $device) {
         // Get device token
         $device_token = $device->getDeviceToken();
         // Setting remote socket
         $remote_path = 'ssl://gateway.push.apple.com:2195';
         // If we are in development environment
         if (PHPush::Environment() == PHPush::ENVIRONMENT_DEVELOPMENT) {
             // Change to development remote socket
             $remote_path = 'ssl://gateway.sandbox.push.apple.com:2195';
         }
         $stream_socket = $this->get_stream_socket($remote_path, $passphrase);
         // Build the binary notification
         $msg = chr(0) . pack('n', 32) . pack('H*', $device_token) . pack('n', strlen($payload)) . $payload;
         // Send it to the server
         fwrite($stream_socket, $msg, strlen($msg));
     }
 }
Пример #2
0
<?php

// Include PHPush
require_once 'vendor/autoload.php';
use PHPush\PHPush;
// Setting environment
PHPush::Environment(PHPush::ENVIRONMENT_PRODUCTION);
// Adding Android key
PHPush::Provider(\PHPush\Provider::PROVIDER_ANDROID)->setAccessKey('test');
// Adding iOS certificate
PHPush::Provider(\PHPush\Provider::PROVIDER_IOS)->setCertificate('ck.pem');
// Creating new queue
$queue = PHPush::Queue();
// Adding some devices
$queue->add(new \PHPush\providers\android\Device('android_registration_id'));
$queue->add(new \PHPush\providers\ios\Device('ios_device_token'));
// Setting message
$queue->message('Hello World!');
// Send message. You can provide custom fields to this method.
// Also you can pass sound and passphrase with this custom fields
$queue->send(array('custom' => 'field', 'sound' => 'popup.aif', 'passphase' => 'phpush'));
// Creating another queue
$another_queue = PHPush::Queue();
// Adding only one device
$another_queue->add(new \PHPush\providers\ios\Device('another_or_the_same_ios_device_token'));
// Setting message
$another_queue->message('Hello World! I\'m second queue!');
// This will not open a connection to APNS server again.
// It will use the old connection
$another_queue->send();