PHP library to send notifications to mobile devices.
If you're using Symfony framework, take a look at LinkValue/MobileNotifBundle to easily manage and profile multiple clients.
Using Composer through composer.json file:
"require": {
"linkvalue/mobile-notif": "^0.2"
}Or using composer CLI:
composer require linkvalue/mobile-notif<?phprequire'path/to/composer/vendor/autoload.php';
useLinkValue\MobileNotif\Client\GcmClient;
useLinkValue\MobileNotif\Model\GcmMessage;
$client = newGcmClient();
$client->setUp(array(
'endpoint' => 'https://android.googleapis.com/gcm/send',
'api_access_key' => 'API ACCESS KEY',
));
$message = newGcmMessage();
$message
->addToken('DESTINATION DEVICE TOKEN HERE')
->setNotificationTitle('Message title')
->setNotificationBody('Message body')
->setNotificationIcon('myicon')
;
$client->push($message);<?phprequire'path/to/composer/vendor/autoload.php';
useLinkValue\MobileNotif\Client\ApnsClient;
useLinkValue\MobileNotif\Model\ApnsMessage;
$client = newApnsClient();
$client->setUp(array(
'endpoint' => 'tls://gateway.sandbox.push.apple.com:2195',
'ssl_pem_path' => __DIR__.'/BundleIncludingCertAndPrivateKey-dev.pem',
));
$message = newApnsMessage();
$message
->addToken('DESTINATION DEVICE TOKEN HERE')
->setSimpleAlert('Hello World!')
;
$client->push($message);Send notification using GCM to multiple devices and log everything in a file using Monolog as Psr/Log implementation
<?phprequire'path/to/composer/vendor/autoload.php';
useLinkValue\MobileNotif\Client\GcmClient;
useLinkValue\MobileNotif\Model\GcmMessage;
useMonolog\Logger;
useMonolog\Handler\StreamHandler;
$log = newLogger('notif');
$log->pushHandler(newStreamHandler(__DIR__.'/notif.log', Logger::INFO));
$client = newGcmClient();
$client
->setLogger($log);
->setUp(array(
'endpoint' => 'https://android.googleapis.com/gcm/send',
'api_access_key' => 'API ACCESS KEY',
))
;
$message = newGcmMessage();
$message
->addToken('DESTINATION DEVICE TOKEN1 HERE')
->addToken('DESTINATION DEVICE TOKEN2 HERE')
->setNotificationTitle('Message title')
->setNotificationBody('Message body')
->setNotificationIcon('myicon')
->setNotificationSound('default')
;
$client->push($message);Send notification using APNS (in production mode) to multiple devices and log everything in a file using Monolog as Psr/Log implementation
<?phprequire'path/to/composer/vendor/autoload.php';
useLinkValue\MobileNotif\Client\ApnsClient;
useLinkValue\MobileNotif\Model\ApnsMessage;
useMonolog\Logger;
useMonolog\Handler\StreamHandler;
$log = newLogger('notif');
$log->pushHandler(newStreamHandler(__DIR__.'/notif.log', Logger::INFO));
$client = newApnsClient();
$client
->setLogger($log);
->setUp(array(
'endpoint' => 'tls://gateway.push.apple.com:2195',
'ssl_pem_path' => __DIR__.'/data/BundleIncludingCertAndPrivateKey-prod.pem',
'ssl_passphrase' => 'my production PEM file passphrase',
))
;
$message = newApnsMessage();
$message
->addToken('DESTINATION DEVICE TOKEN1 HERE')
->addToken('DESTINATION DEVICE TOKEN2 HERE')
->setAlertTitle('Message title')
->setAlertBody('Message body')
->setBadge(1)
->setSound('default')
;
$client->push($message);We want this library to be fully covered by unit tests, so if you contribute to this project, be aware that your PR build will fail if the code coverage drops below 100%.
# install dependencies
composer install
mkdir -p wallet
# if xdebug extension is enabled on PHP CLI
vendor/bin/phpunit --coverage-html wallet/coverage
# else if xdebug is installed but not enabled
php -dzend_extension=xdebug.so vendor/bin/phpunit --coverage-html wallet/coverage
