Skip to content
This repository was archived by the owner on Jul 9, 2019. It is now read-only.

Linkvalue-Interne/MobileNotif

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MobileNotif

Build Status Code Coverage Scrutinizer Code Quality

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.

Installation

Using Composer through composer.json file:

"require": {
    "linkvalue/mobile-notif": "^0.2"
}

Or using composer CLI:

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);

Send notification using GCM to multiple devices and log everything in a file using Monolog as Psr/Log implementation

<?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);

Send notification using APNS (in production mode) to multiple devices and log everything in a file using Monolog as Psr/Log implementation

<?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%.

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

About

Mobile push notification library

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages