PHP library for easy Apple/Android notification message pushing.
- PHP 5.3+
- PHP Curl extension - +SSL support (for AndroidPusher service)
- PHP OpenSSL extension (for ApplePusher service)
Just add sly/notification-pusher
package to the requirements of your Composer JSON configuration file,
and run php composer.phar install
to install it.
Clone this library from Git with git clone https://github.com/Ph3nol/NotificationPusher.git
.
Goto to the library directory, get Composer phar package and install vendors:
curl -s https://getcomposer.org/installer | php
php composer.phar install
You're ready to go.
- Requirements: create a developer SSL certificate for your application
<?php
require_once '/path/to/your/vendor/autoload.php';
use Sly\NotificationPusher\Model\Message;
use Sly\NotificationPusher\Pusher\ApplePusher;
/**
* Initialize Apple pusher service.
*/
$pusher = new ApplePusher(array(
'dev' => true, // Developer/Sandbox mode enabled (default: false)
'simulate' => false, // Simulate sendings (default: false)
'certificate' => '/path/to/your/certificate.pem',
'certificate_passphrase' => 'myPassPhrase' // Generated certificate passphrase (if needed)
));
/**
* Specify some device tokens
*/
$devices = array('D3v1c3T0k3n1', 'D3v1c3T0k3n2', 'D3v1c3T0k3n3'), // Apple Device Tokens
/**
* Add some test pushes.
*/
for ($i = 1; $i <= 3; $i++) {
$message = new Message($devices[$i], sprintf('This is Test #%d', $i));
// $message->setAlert(false); // Don't display message
// $message->setBadge(5); // Increment users badges with '5'
// $message->setSound('bingbong.aiff'); // Set specific sound
$pusher->addMessage($message);
}
/**
* Push queue.
*/
$pushedMessages = $pusher->push();
- Requirements: get a Google account project API key
<?php
require_once '/path/to/your/vendor/autoload.php';
use Sly\NotificationPusher\Model\Message;
use Sly\NotificationPusher\Pusher\AndroidPusher;
/**
* Initialize Android pusher service.
*/
$pusher = new AndroidPusher(array(
'applicationID' => '123456789012', // Your Google project application ID
'apiKey' => 'y0ur4p1k3y' // Your Google account project API key
));
/**
* Specify some device tokens
*/
$devices = array('D3v1c3T0k3n1', 'D3v1c3T0k3n2', 'D3v1c3T0k3n3'), // Android register IDs
/**
* Add some test pushes.
*/
for ($i = 1; $i <= 3; $i++) {
$message = new Message($devices[$i], sprintf('This is Test #%d', $i));
$pusher->addMessage($message);
}
/**
* Push queue.
*/
$pushedMessages = $pusher->push();
This library is using Atoum for unit testing,
whose Composer package can be installed with dev
mode:
php composer install --dev
./atoum -d tests/units
Getting the certificates in place. Reach for your mac and start doing the following:
-
- Login to iPhone Developer Connection Portal and click on App Ids.
-
- Create an AppId for your application withouth a wildcard. It should be something like this: com.vxtindia.PushSample.
-
- Click on configure and then go ahead and create a certificate for Push Notifications. Download it once it has been created.
-
- Import the newly created certificate into your keychain by double clicking it.
-
- Launch "Keychain Assistant" and filter it by the Certificate's category. Then you should see a "Apple Development Push Services" option. Expand it, right click on it, click on "Export..." and save this as apns-dev-cert.p12. Also download the private key as apns-dev-key.p12.
-
- Copy apns-dev-cert.p12 file to your server source code folder.
-
- Now run
openssl pkcs12 -clcerts -nokeys -out apns-dev-cert.pem -in apns-dev-cert.p12
andopenssl pkcs12 -nocerts -out apns-dev-key.pem -in apns-dev-key.p12
on your server.
- Now run
-
- From Ubuntu-9.04 server, we had to remove the passphrase, which can be done with
openssl rsa -in apns-dev-key.pem -out apns-dev-key-noenc.pem
.
- From Ubuntu-9.04 server, we had to remove the passphrase, which can be done with
-
- Finally, combine the two to get your apns-dev.pem file:
cat apns-dev-cert.pem apns-dev-key-noenc.pem > apns-dev.pem
.
- Finally, combine the two to get your apns-dev.pem file:
Source: http://vxtindia.com/blog/push-notifications-for-your-iphone-app-with-php-and-ubuntu/
-
- Go on [https://code.google.com/apis/console](Google APIs console dashboard)
-
- Create a new projet
-
- You are now on your new project homepage, with a URL like
https://code.google.com/apis/console/#project:123456789012
.123456789012
is your application ID.
- You are now on your new project homepage, with a URL like
-
- Click "Api Access" tab to obtain your API key