Skip to content

Instantly share code, notes, and snippets.

@blixit
Last active January 29, 2018 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blixit/c6660d2a8a57e0d519e72c1ee3c89293 to your computer and use it in GitHub Desktop.
Save blixit/c6660d2a8a57e0d519e72c1ee3c89293 to your computer and use it in GitHub Desktop.
Send notification with relief_applications/notifications-bundle

ReliefApps Notifications Bundle

Installation

composer require relief_applications/notifications-bundle

Usage without symfony

  • adapt the script without-symfony.php to your code

Usage with symfony

  • declare the bundle
  • set the configuration
ra_notifications:
    android:
        server_key: "%android_server_key%"
        fcm_server: "%android_fcm_server%"
    ios:
        push_passphrase: '%ios_push_passphrase%'
        push_certificate: '%ios_push_certificate%'
        apns_server: '%ios_apns_server%'
    device:
        class: YourAppBundle\Entity\Device #should implements DeviceInterface or extends Device
        manager: YourAppBundle\Entity\Device #should implements DeviceManagerInterface or extends DeviceManager
    contexts:
        context_1:
            ios:
                push_certificate: /var/ioskeys/context_1.pem
                apns_topic: org.yourapp.context_1
        context_2:
            ios:
                push_certificate: /var/ioskeys/context_2.pem
                apns_topic: org.yourapp.context_2
  • configure the logger
monolog:
    handlers:
        main:
            type: warn
            path: "%kernel.logs_dir%/%kernel.environment%.log"
            level: debug
            channels: [!event]
        console:
            type:   console
            channels: [!event, !doctrine]
<?php
// It could be better to put this code into a symfony service to make the controller less heavy
public function testPuserAction(Request $request)
{
$entityManager = $this->get('doctrine')->getEntityManager();
$deviceManager = DeviceManager::newInstance($entityManager, Device::class);
/** @var ContextManager $contextManager */
$contextManager = $this->get('ra_notifications.context.manager');
$logger = $this->get('logger');
$pusher = new Pusher($contextManager, $logger);
$pusher->onSuccess = function ($response){
var_dump($response);
};
$pusher->onError = function ($error, $message){
var_dump(sprintf("Error : %s => %s", $error, $message));
};
$body = new RANotificationBody();
$body->setTitle("Yo !!");
$targetsMany = $deviceManager->getAll();
$responseStatus = $pusher->pushToMany($body, $targetsMany, "context_1");
$token = 'd_G...WSYM:AP...NoxS0JTXi_qRWxk1-_WYGFGyv_-zmXMpoKoQpyziky...jc3-Hc7THViTE...LABXJ5rIL';
$responseStatus = $pusher->pushToOne($body, $token, "context_1");
$responseStatus = $pusher->pushToOne($body, $targetsMany[0], "context_1");
$targetsGroup = '/topics/anytopic';
$responseStatus = $pusher->pushToGroup($body, $targetsGroup, "context_1");
return new JsonResponse(...);
}
<?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());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment