2017 © Pedro Peláez
 

library push-notifications

Push Notifications for iOS (APNS), Android (GCM/FCM) and Windows (WNS)

image

ricwein/push-notifications

Push Notifications for iOS (APNS), Android (GCM/FCM) and Windows (WNS)

  • Wednesday, February 28, 2018
  • by ricwein
  • Repository
  • 2 Watchers
  • 13 Stars
  • 518 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 2 Versions
  • 12 % Grown

The README.md

PushNotification

... is a small php-library to wrap Apple (APNS) and Google (FCM) Push-Notifications into a simple syntax., (*1)

Examples:, (*2)

Android

use ricwein\PushNotification\{PushNotification, Message, Handler};

$message = new Message('message', 'title', ['payload' => 'data']);
$fcm = new Handler\FCM('ExampleGooglePushToken12345678987654321');

$push = new PushNotification(['fcm' => $fcm]);
$push->send($message, ['<device-token>' => 'fcm']);

iOS

NOTE: The APNS Handler uses the new apple push servers, which require HTTP2. Therefore, curl with HTTP2 support must be installed., (*3)

use ricwein\PushNotification\{PushNotification, Message, Handler, Config};
use Pushok\AuthProvider;

$message = new Message('message', 'title', ['payload' => 'data']);
$apns = new Handler\APNS(AuthProvider\Token::create([
    '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
]), Config::ENV_PRODUCTION);

$push = new PushNotification(['apns' => $apns]);
$push->send($message, ['<device-token>' => 'apns']);

mixed

Sending messages to multiple devices of difference operating systems is also simple:, (*4)

use ricwein\PushNotification\{PushNotification, Message, Handler, Config};

$message = new Message('message', 'title');
$fcm = new Handler\FCM('ExampleGooglePushToken12345678987654321');
$apns = new Handler\APNS($authToken, Config::ENV_PRODUCTION);

$push = new PushNotification(['apns' => $apns, 'fcm' => $fcm]);
$push->send($message, [
    '<ios-device-token1>' => 'apns',
    '<ios-device-token2>' => 'apns',
    '<android-device-token1>' => 'fcm',
    '<android-device-token2>' => 'fcm',
]);

single message / single handler

For single handler messages it's possible to inline the handler into the device-destination array. The handler is then freed automatically after the message was send., (*5)

use ricwein\PushNotification\{PushNotification, Message, Handler};

$result = (new PushNotification)->send(new Message($body,$title), [
    '<device-token>' => new Handler\<APNS/FCM/etc.>(...$config);
]);

usage

This class uses the root-namespace ricwein\PushNotification., (*6)

init

The libraries main class is called PushNotification and requires an array of available push-handlers for the constructor. It's possible to set an ID as the handlers array key, to allow assigning devices to the handler later on., (*7)

Available push-handler are:, (*8)

  • Apple: PushNotification\Handler\APNS
  • Google: PushNotification\Handler\FCM

They're all extending PushNotification\Handler, (*9)

configuration

Since all push-settings are push-handler specific, the settings are directly applied in the handler constructors., (*10)

  • APNS:
 new APNS(
    \Pushok\AuthProviderInterface $authProvider, /* @see https://github.com/edamov/pushok/blob/master/README.md#getting-started */
    string $environment /* (Config::ENV_PRODUCTION / Config::ENV_DEVELOPMENT / Config::ENV_CUSTOM) */
)
 ```

- FCM:
```php
 new FCM(
    string $token,
    ?string $caCertPath = null,
    string $url = self::FCM_ENDPOINT,
    int $timeout = 10
)
 ```

It's also possible to have multiple push-handlers with different configurations like:

```php
use ricwein\PushNotification\{PushNotification, Message, Handler, Config};

// @see https://github.com/edamov/pushok
$apnsProd = new Handler\APNS($tokenProd, Config::ENV_PRODUCTION);
$apnsDev = new Handler\APNS($tokenDev, Config::ENV_DEVELOPMENT);

$message = new Message('message', 'title');
$push = new PushNotification(['prod' => $apnsProd, 'dev' => $apnsDev]);

$push->send($message, [
    '<ios-device-token1>' => 'prod',
    '<ios-device-token2>' => 'dev',
]);

sending

Sending is either available for a message object or a raw payload., (*11)

  • A message object is translated into a native push-notification message with body and title for FCM or APNS before sending.
  • A raw payload (array) is sent 'as it is' which might not be a good idea, if you want to mix APNS and FCM in one request.
use ricwein\PushNotification\{Message, Config};

$devices = [...];

$message = new Message('body', 'title');
$message->setSound('test.aiff')->setBadge(2)->setPriority(Config::PRIORITY_NORMAL);
$push->send($message, $devices);

/* OR */

$payload = [...];
$push->sendRaw($payload, $devices);

error handling

The PushNotification::send() method returns an Result object. This usually contains an array of per device errors. If everything succeeded, the entry is null. You can fetch failed device-messages with:, (*12)

$result = $push->send($message, [...]);
$errors = $result->getFailed(); 

Errors are handled as Exceptions, so it's possible to just throw them. To simply just throw the first error if one occurred, call:, (*13)

$push->send($message, [...])->throwOnFirstException();

Be aware: Sometimes other failures than usage-errors occur. APNS and FCM can respond with explicit reasons, which will be handled as ResponseReasonException. It's a good idea to not just throw them (away), but handle them other ways. E.g. you might want to delete or update device-tokens which were marked as invalid., (*14)

use \ricwein\PushNotification\Exceptions\ResponseReasonException;

foreach($result->getFailed() as $token => $error) {
    if ($error instanceof ResponseReasonException) {
        if ($error->isInvalidDeviceToken()) {
            // $token was invalid
        } elseif ($error->isRateLimited()) {
            // the $token device got too many notifications and is currently rate-limited => better wait some time before sending again.
        }       

    }
}

The Versions

28/02 2018

dev-master

9999999-dev

Push Notifications for iOS (APNS), Android (GCM/FCM) and Windows (WNS)

  Sources   Download

MIT

The Requires

  • php >= 7.0.0

 

by Richard Weinhold

28/02 2018

1.0

1.0.0.0

Push Notifications for iOS (APNS), Android (GCM/FCM) and Windows (WNS)

  Sources   Download

MIT

The Requires

  • php >= 7.0.0

 

by Richard Weinhold