MobileNotif
, (*1)
PHP library to send notifications to mobile devices., (*2)
If you're using Symfony framework, take a look at LinkValue/MobileNotifBundle to easily manage and profile multiple clients., (*3)
Installation
Using Composer through composer.json
file:, (*4)
"require": {
"linkvalue/mobile-notif": "^0.2"
}
Or using composer CLI:, (*5)
composer require linkvalue/mobile-notif
Examples
Send simple notification using Google Cloud Messaging (aka. GCM)
<?php
require 'path/to/composer/vendor/autoload.php';
use LinkValue\MobileNotif\Client\GcmClient;
use LinkValue\MobileNotif\Model\GcmMessage;
$client = new GcmClient();
$client->setUp(array(
'endpoint' => 'https://android.googleapis.com/gcm/send',
'api_access_key' => 'API ACCESS KEY',
));
$message = new GcmMessage();
$message
->addToken('DESTINATION DEVICE TOKEN HERE')
->setNotificationTitle('Message title')
->setNotificationBody('Message body')
->setNotificationIcon('myicon')
;
$client->push($message);
Send simple notification using Apple Push Notification Service (aka. APNS) in development mode
<?php
require 'path/to/composer/vendor/autoload.php';
use LinkValue\MobileNotif\Client\ApnsClient;
use LinkValue\MobileNotif\Model\ApnsMessage;
$client = new ApnsClient();
$client->setUp(array(
'endpoint' => 'tls://gateway.sandbox.push.apple.com:2195',
'ssl_pem_path' => __DIR__.'/BundleIncludingCertAndPrivateKey-dev.pem',
));
$message = new ApnsMessage();
$message
->addToken('DESTINATION DEVICE TOKEN HERE')
->setSimpleAlert('Hello World!')
;
$client->push($message);
<?php
require 'path/to/composer/vendor/autoload.php';
use LinkValue\MobileNotif\Client\GcmClient;
use LinkValue\MobileNotif\Model\GcmMessage;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$log = new Logger('notif');
$log->pushHandler(new StreamHandler(__DIR__.'/notif.log', Logger::INFO));
$client = new GcmClient();
$client
->setLogger($log);
->setUp(array(
'endpoint' => 'https://android.googleapis.com/gcm/send',
'api_access_key' => 'API ACCESS KEY',
))
;
$message = new GcmMessage();
$message
->addToken('DESTINATION DEVICE TOKEN1 HERE')
->addToken('DESTINATION DEVICE TOKEN2 HERE')
->setNotificationTitle('Message title')
->setNotificationBody('Message body')
->setNotificationIcon('myicon')
->setNotificationSound('default')
;
$client->push($message);
<?php
require 'path/to/composer/vendor/autoload.php';
use LinkValue\MobileNotif\Client\ApnsClient;
use LinkValue\MobileNotif\Model\ApnsMessage;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$log = new Logger('notif');
$log->pushHandler(new StreamHandler(__DIR__.'/notif.log', Logger::INFO));
$client = new ApnsClient();
$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 = new ApnsMessage();
$message
->addToken('DESTINATION DEVICE TOKEN1 HERE')
->addToken('DESTINATION DEVICE TOKEN2 HERE')
->setAlertTitle('Message title')
->setAlertBody('Message body')
->setBadge(1)
->setSound('default')
;
$client->push($message);
Resources
APNS
GCM
Tests
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%., (*6)
Running tests with HTML code coverage
# 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