Skip to content

Commit

Permalink
Updated Readme and added 2 API fixes
Browse files Browse the repository at this point in the history
* Updated Readme
* Changed order of arguments of AppleConnection to be consistent with behaviour of Android and Windows
* Changed timeout of AndroidConnection to default to 30 seconds and is no longer required

Signed-off-by: Jeroen v.d. Gulik <jeroen@isset.nl>
  • Loading branch information
jeroenvdgulik committed Apr 4, 2017
1 parent cbfb501 commit dc81102
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 11 deletions.
22 changes: 22 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# EditorConfig is awesome: http://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true

# Set default charset
[*.{php,yml,json}]
charset = utf-8

# PSR-2
[*.php]
indent_style = space
indent_size = 4

[*.{yml,json}]
indent_style = space
indent_size = 2
47 changes: 47 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Changelog

## 1.1.0 - 2017-04-04

### Added

* Added Simple Readme
* Added Editorconfig

### Changed

* Changed order of arguments of AppleConnection to be consistent with behaviour of Android and Windows
* Changed timeout of AndroidConnection to default to 30 seconds and is no longer required

## 1.0.4 - 2017-04-03

### Added

* Added Test Suite
* Added Code Coverage
* Added Static Code Analyzer
* Added Travis CI
* Added Coding Style

## 1.0.3 - 2017-03-09

### Changed

* Windows Connections will not init as the default connection to be consistent with behaviour of Apple and Android

## 1.0.2 - 2017-03-08

### Added

* Added MIT License

## 1.0.1 - 2017-03-08

### Fixed

* Renamed package for packagist.org

## 1.0.0 - 2017-03-08

### Added

* Initial release
126 changes: 125 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,125 @@
# IssetBVPushNotification
# IssetBV/PushNotification

[![Build Status](https://travis-ci.org/Isset/pushnotification.svg?branch=master)](https://travis-ci.org/Isset/pushnotification)
[![Coverage Status](https://coveralls.io/repos/github/Isset/pushnotification/badge.svg?branch=master)](https://coveralls.io/github/Isset/pushnotification?branch=master)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
[![PHPStan](https://img.shields.io/badge/PHPStan-enabled-brightgreen.svg?style=flat)](https://github.com/phpstan/phpstan)


PushNotification is a push message abstraction which allows you to easily send push notifications to mobile devices. Currently we support Apple, Android and Windows (experimental)

## Why Yet Another Notification Package

* There are many packages out there that support mobile push notification, but most do not support an unified API.
* Some implementations were lacking flexible logging capabilities
* Most implementations did not support batches/queues
* Most implementations did not take into account that when sending batches to Apple, if one of the messages fails, the entire batch is dropped. This means that if you have a queue of 50 messages and the 3rd message fails, 47 messages will be dropped. This frustrated us to no end, so we build a simple yet effective fallback mechanism that will restart the batch from the first message after the failed one.

## Goals

* Have a generic API for sending push notifications regardless of device.
* Have consistent output.
* Integrate well with other packages/frameworks.
* Have a flexible logging mechanism.
* Have build in queue mechanism.
* Have build in queue resume when dealing with batches (specifically sent to Apple).

## Prerequisites

* PHP 7.0+
* [cURL](https://secure.php.net/manual/en/book.curl.php)


## Installation

Through Composer:

```bash
composer require issetbv/push-notification
```

## Integrations

At the moment we only support Symfony, since that's what we use, but feel free to create your own, and send us a PR with a link to your integration

* Symfony integration: https://github.com/Isset/pushnotification-bundle

## Supported Devices

* Android
* Apple
* Windows (experimental)

## Documentation

The bulk of the documentation is stored in the docs/index.md file in this package:

[Read the Documentation](docs/index.md)

If you just want to send a message without using the `NotificationCenter` here are some simple `TL;DR` examples

### Simple Android Example

To send a push notification to an Android device we first need to setup a connection. A connection needs a `name`, `api url` and your `api key`. Lastly we need the device token of the device we want to send the message to.

```php
use IssetBV\PushNotification\Type\Android\AndroidConnection;
use IssetBV\PushNotification\Type\Android\Message\AndroidMessage;

$connection = new AndroidConnection(
$name, // 'android'
$api_url, // 'https://fcm.googleapis.com/fcm/send'
$api_key, // 'super-secret-api-key
);

$message = new AndroidMessage('my-device-token');
$message->addToPayload('notification', ['title' => 'Test android']);

$response = $connection->sendAndReceive($message);

echo $response->isSuccess(); // should be true
```

### Simple Apple Example

To send a push notification to an Apple device we first need to setup a connection. A connection needs a `name`, `api url`, location of the `pem file` and the passphrase of the `pem` file (if the `pem` file has one). Lastly we need the device identifier of the device we want to send the message to.

```php
use IssetBV\PushNotification\Type\Apple\AppleConnection;
use IssetBV\PushNotification\Type\Apple\Message\AppleMessageAps;

$connection = new AppleConnection(
$name, // 'apple'
$api_url, // 'ssl://gateway.push.apple.com:2195'
$pemFile, // __DIR__ '/pemfile.pem'
$passPhrase // 'super-secret-passphrase'
);

$appleMessage = new AppleMessageAps('my-device-identifier');
// see notes below as to why we don't use ->addToPayload()
$appleMessage->getAps()->setAlert('Test apple');

$response = $connection->sendAndReceive($appleMessage);

echo $response->isSuccess(); // should be true
```

When sending messages to Apple, the payload can have a specific key named `aps` where we can specify that it should show a notification on the screen. For more information, visit the [official Apple documentation](https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html) regarding the message format.

### Simple Windows Example

To send a push notification to a Windows device we first need to setup a connection. A connection only needs a `name`. Differently than with Android or Apple, the Windows message needs the uri of your specific device to deliver the message.

```php
use IssetBV\PushNotification\Type\Windows\Message\WindowsMessage;
use IssetBV\PushNotification\Type\Windows\WindowsConnection;

$connection = new WindowsConnection('windows');

$windowsMessage = new WindowsMessage('https://cloud.notify.windows.com/?token=AQE%bU%2fSjZOCvRjjpILow%3d%3d');
$windowsMessage->addToPayload('wp:Text1', 'Test Windows');

$response = $connection->sendAndReceive($windowsMessage);

echo $response->isSuccess(); // should be true
```
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "issetbv/push-notification",
"description": "",
"description": "PushNotification is a push message abstraction which allows you to easily send push notifications to mobile devices. Currently we support Apple, Android and Windows (experimental)",
"keywords": ["Notifications", "Push Notifications", "Push Messages", "Push", "Mobile", "Apple", "Android", "Windows", "iOS", "WindowsPhone"],
"homepage": "http://www.isset.nl",
"license": "MIT",
"authors": [
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Documentation
2 changes: 1 addition & 1 deletion src/PushNotification/Type/Android/AndroidConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class AndroidConnection implements Connection
* @param bool $default
* @param null|HandlerStack $handler
*/
public function __construct(string $type, string $apiUrl, string $apiKey, int $timeout, bool $dryRun = false, bool $default = false, HandlerStack $handler = null)
public function __construct(string $type, string $apiUrl, string $apiKey, int $timeout = 30, bool $dryRun = false, bool $default = false, HandlerStack $handler = null)
{
$clientConfig = [
'base_uri' => rtrim($apiUrl, '/'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use IssetBV\PushNotification\Core\Message\Message;

/**
* Class AndroidMessage.
* @see https://firebase.google.com/docs/cloud-messaging/send-message.
*/
class AndroidMessage implements Message
{
Expand Down
7 changes: 2 additions & 5 deletions src/PushNotification/Type/Apple/AppleConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
use IssetBV\PushNotification\LoggerTrait;
use IssetBV\PushNotification\Type\Apple\Message\AppleMessage;

/**
* Class AppleConnection.
*/
class AppleConnection implements Connection
{
use LoggerTrait;
Expand Down Expand Up @@ -52,13 +49,13 @@ class AppleConnection implements Connection
/**
* Connection constructor.
*
* @param string $url
* @param string $type
* @param string $url
* @param string $pemKeyFile
* @param string $pemPasswordPhrase
* @param bool $default
*/
public function __construct(string $url, string $type, string $pemKeyFile, string $pemPasswordPhrase = null, bool $default = false)
public function __construct(string $type, string $url, string $pemKeyFile, string $pemPasswordPhrase = null, bool $default = false)
{
$this->type = $type;
$this->default = $default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace IssetBV\PushNotification\Type\Apple\Message;

/**
* Class AppleMessageAps.
* @see https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html
*/
class AppleMessageAps extends AppleMessage
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use IssetBV\PushNotification\Core\Message\Message;

/**
* Class WindowsMessage.
* @see https://msdn.microsoft.com/en-us/library/windows/apps/hh202967(v=vs.105).aspx
*/
class WindowsMessage implements Message
{
Expand Down

0 comments on commit dc81102

Please sign in to comment.