Git Product home page Git Product logo

pushnotifications's Introduction

PushNotification

... is a small php-library to wrap Apple, Google and Windows Push-Notifications into a simple syntax.

Examples:

Android:

<?php

use PushNotification\PushNotification;
use PushNotification\Handler\GCMHandler;

$push = new PushNotification(new GCMHandler());
$push->setServerToken('ExampleGooglePushToken12345678987654321');
$push->addDevice('device-token');
$push->send('message', ['payload' => 'data']);

?>

iOS:

<?php

use PushNotification\PushNotification;
use PushNotification\Handler\APNSHandler;

$push = new PushNotification(new APNSHandler());
$push->setServer([
	'token' => 'path/to/cert.pem',
	'url'   => 'ssl://gateway.sandbox.push.apple.com:2195',
]);
$push->addDevice('<device-token>');
$push->send('message', ['payload' => 'data']);

?>

Windows:

<?php

use PushNotification\PushNotification;
use PushNotification\Handler\WNSHandler;

$push = new PushNotification(new WNSHandler());
$push->setServer([
	'token' => 'wns-push-token',
	'url'   => 'server.url',
]);
$push->addDevice([
	'clientID' => 'clientSecret',
	'OAuth2-Token',
]);
$push->send('message', ['payload' => 'data']);

?>

usage:

This class uses the root-namespace PushNotification.

init

It's possible to init the PushNotification class with a specific push-handler:

<?php

use PushNotification\PushNotification;
use PushNotification\Handler\GCMHandler;

$push = new PushNotification(new GCMHandler());

?>

or without, and adding the push-handler later:

<?php

use PushNotification\PushNotification;
use PushNotification\Handler\GCMHandler;

$push        = new PushNotification();
$pushHandler = new GCMHandler();
$push->setHandler($pushHandler);

?>

Available push-handler are:

  • Apple: PushNotification\Handler\APNSHandler
  • Google: PushNotification\Handler\GCMHandler
  • Windows: PushNotification\Handler\WNSHandler

They're all extending PushNotification\PushHandler

configuration

Since all push-settings are push-handler specific, the according handler has to been added to the PushNotification class before applying the configuration at the PushNotification.

Settings as server-token and server-url can be set like:

<?php

$push->setServerToken('server-token');
$push->setServerUrl('server-url');

?>

or as an array:

<?php

$push->setServer([
	'token' => 'server-token',
	'url'   => 'server-url',
]);

?>

It's also possible to set the configuration directly at the push-handler:

<?php

$pushHandler->setServerToken('server-token');
$pushHandler->setServerUrl('server-url');

// or:
$pushHandler->setServer([
	'token' => 'server-token',
	'url'   => 'server-url',
]);

// or even:
$pushHandler = new GCMHandler('server-token', 'server-url');

?>

client-devices

The class can send notifications to multiple devices at once. The device-push-tokens are escaped by default.

<?php

$push->addDevice('push-token1');
$push->addDevice('push-token2');

// or:
$push->addDevice([
	'push-token1',
	'push-token2',
]);

?>

Note: The WNSHandler allows adding either:

  • client-id -> client-secret pairs (as array key & value)
  • or adding directly OAuth2-tokens
  • and even mixed combinations of both

sending

Sending a messages is as simple as that:

<?php

$push->send('message');

?>

It's possible to add a payload as an array:

<?php

$payload = ['data'];
$push->send('message', $payload);

?>

Sometimes the given server or device-tokens are expired or sending simply failed. To check if sending was successfully you can use the return-value of send():

<?php

if (!$push->send('message')) {
	throw new \Exception('oh-no: Sending PushNotifications failed.');
}

?>

Exceptions

This class can throw some default \Exception() - mostly in case of incorrect configuration or unreachable servers.

pushnotifications's People

Contributors

ricwein avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.