|
<?php |
|
|
|
require __DIR__ . '/../vendor/autoload.php'; |
|
$autoloader = require_once __DIR__ . '/../vendor' . DIRECTORY_SEPARATOR . 'autoload.php'; |
|
|
|
|
|
use Monolog\Logger; |
|
use RA\NotificationsBundle\Model\Configuration\Configuration; |
|
use RA\NotificationsBundle\Model\Context\ContextManager; |
|
use RA\NotificationsBundle\Model\Device\Device; |
|
use RA\NotificationsBundle\Model\Device\DeviceManager; |
|
use RA\NotificationsBundle\Model\Notification\NotificationBody; |
|
use RA\NotificationsBundle\Providers\Pusher; |
|
use RA\NotificationsBundle\Providers\PusherException; |
|
|
|
// Declare a custom class |
|
class CustomDevice extends Device{ |
|
|
|
} |
|
|
|
// Declare a logger. it's required by the pusher to log error |
|
$logger = new Logger('name'); |
|
$logger->pushHandler(new Monolog\Handler\StreamHandler('log/app.log', Monolog\Logger::WARNING)); |
|
$logger->addWarning('Foo'); |
|
|
|
// Declare the context with required parameters |
|
$contextManager = new ContextManager( |
|
'AIzaSy4561...R9PgEhZwuzknK7...', //$android_server_key |
|
'fcm.googleapis.com', //$android_fcm_server |
|
'__', //$ios_push_passphrase |
|
'__', //$ios_push_certificate |
|
'api.development.push.apple.com' , //$ios_apns_server |
|
'__' , //$ios_apns_topic |
|
'__' , //$ios_protocol |
|
CustomDevice::class, //$device_class => DeviceInterface |
|
DeviceManager::class, //$device_manager_class => DeviceManagerInterface |
|
[ |
|
'context_1' => [ |
|
'ios' => [ |
|
'push_certificate' => '/var/ioskeys/context_1.pem', |
|
'apns_topic' => 'org.yourapp.context_1', |
|
] |
|
] |
|
|
|
] |
|
); |
|
|
|
// Declare the pusher |
|
$pusher = new Pusher($contextManager, $logger); |
|
|
|
// Declare callbacks to handle requests on success and error |
|
$pusher->onSuccess = function ($response){ |
|
var_dump($response); |
|
}; |
|
$pusher->onError = function ($error, $message){ |
|
var_dump(sprintf("Error : %s => %s", $error, $message)); |
|
}; |
|
|
|
// Declare the notification |
|
$body = new NotificationBody(); |
|
$body->setTitle("New features here ! Come take a look !!"); |
|
$body->setBody("We just developed a new feature that should interest you"); |
|
|
|
// Declare a token (took from the frontend app) |
|
$token = '...d_GI27yWSYM:APA91bGDjnSxjvcoNox...RWxk1-_WYGFGyv_-zmXMpoKoXOo0dobmKJmjc-ABXJ5rIL...'; |
|
// Declare a device with this token |
|
$singleDevice = (new CustomDevice()) |
|
->setToken($token) |
|
->setPushEnabled(1); // default value is 'true' |
|
|
|
try{ |
|
/** |
|
* if the context is not defined, then the first context from the configuration will be used |
|
*/ |
|
|
|
//simulate an array i=of devices |
|
$targetsMany = [ $singleDevice ]; |
|
//push to many devices |
|
$count = $pusher->pushToMany($body, $targetsMany, "context_1"); |
|
|
|
//push to a single device |
|
$count = $pusher->pushToOne($body, $token, "context_1"); |
|
$count = $pusher->pushToOne($body, $singleDevice, "context_1"); |
|
|
|
//push to a group or a topic |
|
$targetsGroup = '/topics/anytopic'; |
|
$count = $pusher->pushToGroup($body, $targetsGroup, "context_1"); |
|
|
|
}catch (PusherException $e){ |
|
var_dump($e->getMessage()); |
|
} |
|
|