exeerik/pushok

Customized PHP client for Apple Push Notification Service (APNs) - Send push notifications to iOS using the new APNs HTTP/2 protocol with token-based (JWT with p8 private key) or certificate-based authentication

0.11.1 2020-04-02 16:46 UTC

README

PHP >= 7.2 Latest Version on Packagist Total Downloads Software License

Pushok is a simple PHP library for sending push notifications to APNs.

Features

  • Uses new Apple APNs HTTP/2 connection
  • Supports JWT-based authentication
  • Supports Certificate-based authentication
  • Supports new iOS 10 features such as Collapse IDs, Subtitles and Mutable Notifications
  • Uses concurrent requests to APNs
  • Tested and working in APNs production environment
  • Critical alerts functionality

Requirements

  • PHP >= 7.2
  • lib-curl >= 7.46.0 (with http/2 support enabled)
  • lib-openssl >= 1.0.2e

Install

Via Composer

$ composer require edamov/pushok

Getting Started

<?php
require __DIR__ . '/vendor/autoload.php';

use Pushok\AuthProvider;
use Pushok\Client;
use Pushok\Notification;
use Pushok\Payload;
use Pushok\Payload\Alert;
use Pushok\Payload\Sound;

$options = [
    'key_id' => 'AAAABBBBCC', // The Key ID obtained from Apple developer account
    'team_id' => 'DDDDEEEEFF', // The Team ID obtained from Apple developer account
    'app_bundle_id' => 'com.app.Test', // The bundle ID for app obtained from Apple developer account
    'private_key_path' => __DIR__ . '/private_key.p8', // Path to private key
    'private_key_secret' => null // Private key secret
];

$authProvider = AuthProvider\Token::create($options);

$alert = Alert::create()->setTitle('Hello!');
$alert = $alert->setBody('First push notification');

$payload = Payload::create()->setAlert($alert);

//set notification sound to default
$payload->setSound('default');

//if you want to send a critical alert you need to specify the following settings
//and remove previous set "$payload->setSound('default');"
$sound = Sound::create()->setCriticalSoundEnabled(1);
$sound = $sound->setCriticalSoundName('default');
$sound = $sound->setCriticalSoundVolume(1.0);
$payload->setSound($sound); 

//add custom value to your notification, needs to be customized
$payload->setCustomValue('key', 'value');

$deviceTokens = ['<device_token_1>', '<device_token_2>', '<device_token_3>'];

$notifications = [];
foreach ($deviceTokens as $deviceToken) {
    $notifications[] = new Notification($payload,$deviceToken);
}

$client = new Client($authProvider, $production = false);
$client->addNotifications($notifications);



$responses = $client->push(); // returns an array of ApnsResponseInterface (one Response per Notification)

foreach ($responses as $response) {
    $response->getApnsId();
    $response->getStatusCode();
    $response->getReasonPhrase();
    $response->getErrorReason();
    $response->getErrorDescription();
}

Testing

$ composer test

Security

If you discover any security related issues, please email erik@exeerik.de instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.