Skip to content

Commit

Permalink
Update to Pusher Beams (#42)
Browse files Browse the repository at this point in the history
* Update to latest phpunit and mockery

* Fix test expectations

* Linting

* Test with PHP 7.4

* Remove unnecessary test tear down

* Switch to Beams SDK

* Automatically register service provider

* Update README

* Remove empty values from payload

* Update README to reflect the change to Pusher Beams

* Rename channel class back to PusherChannel

* Update Docblock
  • Loading branch information
jessarcher authored and atymic committed Jan 3, 2020
1 parent 330865c commit b1ea0f6
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 62 deletions.
45 changes: 23 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Pusher push notifications channel for Laravel 5.5+ & 6.0
# Pusher Beams push notifications channel for Laravel 5.5+ & 6.0

[![Latest Version on Packagist](https://img.shields.io/packagist/v/laravel-notification-channels/pusher-push-notifications.svg?style=flat-square)](https://packagist.org/packages/laravel-notification-channels/pusher-push-notifications)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
Expand All @@ -9,7 +9,11 @@
[![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/laravel-notification-channels/pusher-push-notifications/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/laravel-notification-channels/pusher-push-notifications/?branch=master)
[![Total Downloads](https://img.shields.io/packagist/dt/laravel-notification-channels/pusher-push-notifications.svg?style=flat-square)](https://packagist.org/packages/laravel-notification-channels/pusher-push-notifications)

This package makes it easy to send [Pusher push notifications](https://pusher.com/docs/push_notifications) with Laravel.
This package makes it easy to send [Pusher Beams push notifications](https://pusher.com/docs/beams) with Laravel.

Please note that this notification channel should not be confused with Pusher Channels.

Also please note that prior to version 2.0, this package integrated with Pusher's beta push notifications service that was part of Pusher Channels. Please see Pusher's [migration guide](https://www.pusher.com/docs/channels/push_notifications/migration-guide) for more information.

## Contents

Expand All @@ -33,27 +37,24 @@ You can install the package via composer:
composer require laravel-notification-channels/pusher-push-notifications
```

You must install the service provider:

```php
// config/app.php
'providers' => [
...
NotificationChannels\PusherPushNotifications\PusherPushNotificationsServiceProvider::class,
],
```

### Setting up your Pusher account

Before using this package you should set up a Pusher account. Here are the steps required.

- Login to https://dashboard.pusher.com/
- Select your app from the sidebar or create a new app.
- Click on the "Push Notifications" tab.
- Upload your APNS Certificate or add your GCM API key.
- Now select the "App Keys" tab.
- Copy your `app_id`, `key`, and `secret`.
- Update the values in your `config/broadcasting.php` file under the pusher connection.
Before using this package you should set up a Pusher Beams account. Here are the steps required.

- Login to https://dash.pusher.com/
- Select the "Beams" product.
- Select your instance from the list or create a new instance.
- Click on the "Settings" tab.
- Upload your APNS Certificate and/or add your FCM Server key.
- Now select the "Credentials" tab.
- Copy your `Instance Id`, and `Secret Key`.
- Add a new entry to in your `config/services.php` file:
```php
'pusher' => [
'beams_instance_id' => 'Your Instance Id',
'beams_secret_key' => 'Your Secret Key',
],
```
- You're now good to go.

## Usage
Expand Down Expand Up @@ -93,7 +94,7 @@ class AccountApproved extends Notification
- `sound('')`: Accepts a string value for the notification sound file. Notice that if you leave blank the default sound value will be `default`.
- `icon('')`: Accepts a string value for the icon file. (Android Only)
- `badge(1)`: Accepts an integer value for the badge. (iOS Only)
- `setOption($key, $value)`: Allows you to set any value in the message payload. For more information [check here for iOS](https://pusher.com/docs/push_notifications/ios/server), [or here for Android](https://pusher.com/docs/push_notifications/android/server).
- `setOption($key, $value)`: Allows you to set any value in the message payload. See the [request body section of the Pusher Beam docs](https://pusher.com/docs/beams/reference/publish-api#request-body) for more information.

### Sending to multiple platforms

Expand Down
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"illuminate/notifications": "~5.5 || ~6.0",
"illuminate/queue": "~5.5 || ~6.0",
"illuminate/support": "~5.5 || ~6.0",
"pusher/pusher-php-server": "~3.0 || ~4.0"
"pusher/pusher-push-notifications": "^1.1"
},
"require-dev": {
"mockery/mockery": "^1.3",
Expand All @@ -58,5 +58,12 @@
},
"config": {
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
"NotificationChannels\\PusherPushNotifications\\PusherPushNotificationsServiceProvider"
]
}
}
}
31 changes: 16 additions & 15 deletions src/PusherChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,28 @@
use Illuminate\Events\Dispatcher;
use Illuminate\Notifications\Events\NotificationFailed;
use Illuminate\Notifications\Notification;
use Pusher\Pusher;
use Pusher\PushNotifications\PushNotifications;
use Throwable;

class PusherChannel
{
/**
* @var Pusher
* @var PushNotifications
*/
protected $pusher;
protected $beamsClient;

/**
* @var \Illuminate\Events\Dispatcher
*/
private $events;

/**
* @param Pusher $pusher
* @param PushNotifications $beamsClient
* @param Dispatcher $events
*/
public function __construct(Pusher $pusher, Dispatcher $events)
public function __construct(PushNotifications $beamsClient, Dispatcher $events)
{
$this->pusher = $pusher;
$this->beamsClient = $beamsClient;
$this->events = $events;
}

Expand All @@ -41,15 +43,14 @@ public function send($notifiable, Notification $notification)
$interest = $notifiable->routeNotificationFor('PusherPushNotifications')
?: $this->interestName($notifiable);

$response = $this->pusher->notify(
is_array($interest) ? $interest : [$interest],
$notification->toPushNotification($notifiable)->toArray(),
true
);

if (! in_array($response['status'], [200, 202])) {
$this->events->fire(
new NotificationFailed($notifiable, $notification, 'pusher-push-notifications', $response)
try {
$this->beamsClient->publishToInterests(
is_array($interest) ? $interest : [$interest],
$notification->toPushNotification($notifiable)->toArray()
);
} catch (Throwable $exception) {
$this->events->dispatch(
new NotificationFailed($notifiable, $notification, 'pusher-push-notifications')
);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/PusherMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,13 @@ public function toiOS()
public function toAndroid()
{
$message = [
'gcm' => [
'notification' => [
'fcm' => [
'notification' => array_filter([
'title' => $this->title,
'body' => $this->body,
'sound' => $this->sound,
'icon' => $this->icon,
],
]),
],
];

Expand Down
15 changes: 7 additions & 8 deletions src/PusherPushNotificationsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace NotificationChannels\PusherPushNotifications;

use Illuminate\Support\ServiceProvider;
use Pusher\Pusher;
use Pusher\PushNotifications\PushNotifications;

class PusherPushNotificationsServiceProvider extends ServiceProvider
{
Expand All @@ -13,15 +13,14 @@ class PusherPushNotificationsServiceProvider extends ServiceProvider
public function boot()
{
$this->app->when(PusherChannel::class)
->needs(Pusher::class)
->needs(PushNotifications::class)
->give(function () {
$pusherConfig = config('broadcasting.connections.pusher');
$config = config('services.pusher');

return new Pusher(
$pusherConfig['key'],
$pusherConfig['secret'],
$pusherConfig['app_id']
);
return new PushNotifications([
'instanceId' => $config['beams_instance_id'],
'secretKey' => $config['beams_secret_key'],
]);
});
}
}
11 changes: 6 additions & 5 deletions tests/ChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace NotificationChannels\PusherPushNotifications\Test;

use Exception;
use Illuminate\Events\Dispatcher;
use Illuminate\Notifications\Events\NotificationFailed;
use Illuminate\Notifications\Notifiable;
Expand All @@ -10,13 +11,13 @@
use Mockery\Adapter\Phpunit\MockeryTestCase;
use NotificationChannels\PusherPushNotifications\PusherChannel;
use NotificationChannels\PusherPushNotifications\PusherMessage;
use Pusher\Pusher;
use Pusher\PushNotifications\PushNotifications;

class ChannelTest extends MockeryTestCase
{
public function setUp(): void
{
$this->pusher = Mockery::mock(Pusher::class);
$this->pusher = Mockery::mock(PushNotifications::class);

$this->events = Mockery::mock(Dispatcher::class);

Expand All @@ -34,7 +35,7 @@ public function it_can_send_a_notification()

$data = $message->toArray();

$this->pusher->shouldReceive('notify')->with(['interest_name'], $data, true)->andReturn(['status' => 202]);
$this->pusher->shouldReceive('publishToInterests')->once()->with(['interest_name'], $data);

$this->channel->send($this->notifiable, $this->notification);
}
Expand All @@ -46,9 +47,9 @@ public function it_fires_failure_event_on_failure()

$data = $message->toArray();

$this->pusher->shouldReceive('notify')->with(['interest_name'], $data, true)->andReturn(['status' => 500]);
$this->pusher->shouldReceive('publishToInterests')->once()->with(['interest_name'], $data)->andThrow(new Exception('Something happened'));

$this->events->shouldReceive('fire')->with(Mockery::type(NotificationFailed::class));
$this->events->shouldReceive('dispatch')->once()->with(Mockery::type(NotificationFailed::class));

$this->channel->send($this->notifiable, $this->notification);
}
Expand Down
16 changes: 8 additions & 8 deletions tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function it_provides_a_create_method()
public function by_default_it_will_send_a_message_to_ios()
{
$this->assertTrue(Arr::has($this->message->toArray(), 'apns'));
$this->assertFalse(Arr::has($this->message->toArray(), 'gcm'));
$this->assertFalse(Arr::has($this->message->toArray(), 'fcm'));
}

/** @test */
Expand All @@ -48,11 +48,11 @@ public function it_can_send_a_message_to_the_right_platform()
$this->message->ios();

$this->assertTrue(Arr::has($this->message->toArray(), 'apns'));
$this->assertFalse(Arr::has($this->message->toArray(), 'gcm'));
$this->assertFalse(Arr::has($this->message->toArray(), 'fcm'));

$this->message->android();

$this->assertTrue(Arr::has($this->message->toArray(), 'gcm'));
$this->assertTrue(Arr::has($this->message->toArray(), 'fcm'));
$this->assertFalse(Arr::has($this->message->toArray(), 'apns'));
}

Expand All @@ -69,7 +69,7 @@ public function it_can_set_the_title()

$this->assertEquals('myTitle', Arr::get($this->message->toiOS(), 'apns.aps.alert.title'));

$this->assertEquals('myTitle', Arr::get($this->message->toAndroid(), 'gcm.notification.title'));
$this->assertEquals('myTitle', Arr::get($this->message->toAndroid(), 'fcm.notification.title'));
}

/** @test */
Expand All @@ -79,7 +79,7 @@ public function it_can_set_the_body()

$this->assertEquals('myBody', Arr::get($this->message->toiOS(), 'apns.aps.alert.body'));

$this->assertEquals('myBody', Arr::get($this->message->toAndroid(), 'gcm.notification.body'));
$this->assertEquals('myBody', Arr::get($this->message->toAndroid(), 'fcm.notification.body'));
}

/** @test */
Expand All @@ -89,7 +89,7 @@ public function it_can_set_the_sound()

$this->assertEquals('mySound', Arr::get($this->message->toiOS(), 'apns.aps.sound'));

$this->assertEquals('mySound', Arr::get($this->message->toAndroid(), 'gcm.notification.sound'));
$this->assertEquals('mySound', Arr::get($this->message->toAndroid(), 'fcm.notification.sound'));
}

/** @test */
Expand All @@ -105,7 +105,7 @@ public function it_can_set_the_icon()
{
$this->message->icon('myIcon');

$this->assertEquals('myIcon', Arr::get($this->message->toAndroid(), 'gcm.notification.icon'));
$this->assertEquals('myIcon', Arr::get($this->message->toAndroid(), 'fcm.notification.icon'));
}

/** @test */
Expand All @@ -122,6 +122,6 @@ public function it_can_send_message_to_multiple_platforms()
$this->message->ios()->withAndroid(new PusherMessage());

$this->assertTrue(Arr::has($this->message->toArray(), 'apns'));
$this->assertTrue(Arr::has($this->message->toArray(), 'gcm'));
$this->assertTrue(Arr::has($this->message->toArray(), 'fcm'));
}
}

0 comments on commit b1ea0f6

Please sign in to comment.